blog posts

Learn Variables And Data Types In Java (In Very Simple Language)

Learn Variables And Data Types In Java (In Very Simple Language). In this tutorial, you will learn about variables, how to define them, and the different types of data supported by Java.

Java variables

A variable is a place to store data in memory (storage location). Each variable must be given a unique name (identifier) ​​to indicate the storage location.

How to define variables in Java?

Here is an example to define a variable in Java.

  1. int speedLimit = 80;

Here, speedLimit is an int data variable (integer) with a value of 80 assigned to it.

In this example, we assigned a variable to that value, but this is not mandatory. You can define variables and specify them later without specifying a value. For example,

int speedLimit;

speedLimit = 80;

The value of the variable can be changed in the program, hence it is called “variable”. For example,

int speedLimit = 80;

… ..…

speedLimit = 90;

Java is a static language. That is, all variables must be defined before use.

Also, variable data types cannot be changed in the program. For example, you are unable to do the following:

int speedLimit = 80;

… ..…

float speedLimit;

Rules for naming variables in Java

The Java programming language has a set of specific rules and conventions for naming variables. Here’s what you need to know:

  • Variables in Java are case sensitive.
  • The variable name is a sequence of Unicode letters and numbers and can start with the letter, $ or _. However, the beginning of a variable noun is more common. Also, the variable name cannot contain empty space.

When defining variables, choose a name that makes sense. For example, score, number, level are more meaningful than the names s, n and l.

If you selected a word for a variable name, lowercase all letters. For example, it is better to use speed than SPEED, or sPEED.

If you chose a variable name with more than one word, use all lowercase letters for the first word and capitalize the first letters of each subsequent word. For example, speedLimit.

There are four types of variables in the Java programming language:

  • Sample variables (non-static variables)
  • Class variables (static variables)
  • Local variables
  • Component

Basic Java data types

As mentioned above, Java is a static language. That is, all variables must be defined before use.

int speed;

Here speed is a variable and data type is an int variable. The data type int determines that the variable can only contain integers.

Simply put, the data type of a variable determines the values ​​that a variable can store. There are 8 predefined data types in the Java programming language known as core data types.

In addition to the main data types, there are also reference data types in Java that you will learn about later.

8 main data types

boolean

  • The boolean data type has two possible values, true or false.
  • Default value: false.
  • Usually used for right / wrong conditions.
  • Example:
  1. class BooleanExample {
  2. public static void main (String [] args) {
  3. boolean flag = true;
  4. System.out.println (flag);
  5. }
  6. }

Output

true

byte

  • The byte data type can be between -128 and 127.
  • If the value of the variable is in the range [-128,127], it is used instead of the data type int or other integers.
  • Default value: 0
  • Example:
  1. class ByteExample {
  2. public static void main (String [] args) {
  3. byte range;
  4. range = 124;
  5. System.out.println (range);
  6. // Error code below. Why?
  7. // range = 200
  8. }
  9. }

Output

124

short

  • The short data type can have values ​​from -32768 to 32767 (16-bit two-bit complement integer is signed).
  • If the value of the variable is certainly in the range [-32768, 32767], it is used instead of other types of correct data.
  • Default value: 0
  • Example:
  1. class ShortExample {
  2. public static void main (String [] args) {
  3. short temperature;
  4. temperature = -200;
  5. System.out.println (temperature);
  6. }
  7. }

Output

-200

int

  • Int data type can be values from -2 31 to 2 31 -1 have.
  • If you use Java 8 then, can the 32-bit unsigned integer with a minimum value of 0 and a maximum of 2 31 -1 use.
  • Default value: 0
  • Example:
  1. class IntExample {
  2. public static void main (String [] args) {
  3. int range = -4250000;
  4. System.out.println (range);
  5. }
  6. }

Output

-4250000

long

  • Long data types can range from -2 63 to 2 63-1 .
  • If you use Java 8 or higher, 64-bit unsigned integer can with a minimum value of 0 and a maximum of 2 64 -1 use.
  • Default value: 0
  • Example:
  1. class LongExample {
  2. public static void main (String [] args) {
  3. long range = -42332200000L;
  4. System.out.println (range);
  5. }
  6. }

Output

-42332200000

Note that the use of L at the end of -42332200000 indicates the word long.

double

  • The double data type has a resolution of 64 bits.
  • It should never be used for exact values ​​such as currency.
  • Default value: 0.0 (0.0 d)
  • Example:
  1. class DoubleExample {
  2. public static void main (String [] args) {
  3. double number = -42.3;
  4. System.out.println (number);
  5. }
  6. }

Output

-4.3

float

  • The float data type has a resolution of 32 bits.
  • It should never be used for exact values ​​such as currency.
  • Default value: 0.0 (f 0.0)
  • Example:
  1. class FloatExample {
  2. public static void main (String [] args) {
  3. float number = -42.3f;
  4. System.out.println (number);
  5. }
  6. }

Output

-4.3

Note that in the above program we used -42.3 f instead of -42.3. Because -42.3 is a double. To tell the compiler that’s 42.3 is a float and not a double, you must use f or f.

char

  • The Unicode character is 16-bit.
  • The minimum value is ‘\ u0000’ and the maximum value is ‘\ uffff’.
  • Default value: ‘\ u0000’
  • Example:
  1. class CharExample {
  2. public static void main (String [] args) {
  3. char letter = ‘\ u0051’;
  4. System.out.println (letter);
  5. }
  6. }

Output

Q

The value of Unicode ‘/ u0051’ is equal to Q.

Another example:

  1. class CharExample {
  2. public static void main (String [] args) {
  3. char letter1 = ‘9’;
  4. System.out.println (letter1);
  5. char letter2 = 65;
  6. System.out.println (letter2);
  7. }
  8. }

Output

9

A

When you print letter1, the output is 9 because the letter1 is assigned a 9 character.

When you print letter2, the output is A because the letter value of the letter A is 65.

Java also supports character strings through the java.lang.String class. Here’s how to create a String object in Java:

myString = “Programming is awesome”;

Literal in Java

To understand literal, let’s give an example of assigning a value to a variable.

boolean flag = false;

here,

  • boolean – is a data type.
  • flag – is variable
  • false – is literal.

Litral represents the source code with a fixed value.

Values ​​such as 1.5, 4, true, ‘\ u0050’ that appear directly in the program without the need for calculation are literal.

In the example above, flag is a variable. Because it is boolean, it may be false or true. The compiler needs to calculate to understand it. However, literals such as -5, “a”, true indicate a constant value.

Litral correct

  • The correct literal is used to initialize the variables of data types int, byte, short, int and long.
  • If the correct literal ends with l or L, it is of type long. Note: It is better to use L instead of l.

// Error! literal 42332200000 of type int is out of range

long myVariable1 = 42332200000;

// 42332200000L is of type long, and it’s not out of range

long myVariable2 = 42332200000L;

  • The correct literal can be expressed in decimal, hexadecimal and binary systems.
  • Numbers beginning with the prefix 0x indicate hexadecimal. Similarly, numbers beginning with the prefix 0b indicate binary.

// decimal

int decNumber = 34;

// 0x represents hexadecimal

int hexNumber = 0x2F;

// 0 represents binary

int binNumber = 0b10010;

Litral floating point

  • A floating point literal is used to initialize float and double variables.
  • If a floating point liter ends with f or F, it is float. Otherwise, it is double. The double type can optionally end in D or d. However, it is not necessary.
  • They can also be expressed in scientific symbol numbers using E or e.
  1. class DoubleExample {
  2. public static void main (String [] args) {
  3. double myDouble = 3.4;
  4. float myFloat = 3.4F;
  5. // 3.445 * 10.2
  6. double myDoubleScientific = 3,445e2;
  7. System.out.println (myDouble);
  8. System.out.println (myFloat);
  9. System.out.println (myDoubleScientific);
  10. }
  11. }

Output

  1. 3.4
  2. 3.4
  3. 344.5

Character and string literals

  • Contains Unicode characters (UTF-16).
  • For literal characters, a single quote is used. For example ‘a’ or ‘\ u0111’.
  • For string literals, double quotes are used. And For example, “programming”, “java8 ″
  • Java also supports several special escape sequences. For example, \ b for backspace, \ t for moving cursor to 8 next places, \ n for moving cursor to next line, \ f for moving control to serious page, \ r for beginning of current line, \ ”for Print the quotation mark, \ ‘to print a single quote, and \\ to print a backlash.
  1. class DoubleExample {
  2. public static void main (String [] args) {
  3. char myChar = ‘g’;
  4. char newLine = ‘\ n’;
  5. String myString = “Java 8”;
  6. System.out.println (myChar);
  7. System.out.println (newLine);
  8. System.out.println (myString);
  9. }
  10. }

Output

g

Java