DED9

Learn Variables And Data Types In Java

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 variablesData Types

A variable is where data is stored 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 variable’s value can be changed in the program, 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:

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

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

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

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

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, a variable’s data type determines its ability to store values. The Java programming language has eight predefined data types, known as core data types.

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

8 main data types

Boolean

  1. class BooleanExample {
  2. public static void main (String [] args) {
  3. boolean flag = true;
  4. System.out.println (flag);
  5. }
  6. }

Output

true

byte

  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

  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

  1. class IntExample {
  2. public static void main (String [] args) {
  3. int range = -4250000;
  4. System.out.println (range);
  5. }
  6. }

Output

-4250000

long

  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 using L at the end of -42332200000 indicates the word long.

double

  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

  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 42.3 is a float and not a double, you must use f or f.

char

  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 nine because letter1 is assigned a 9-character String.

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.

Literal represents the source code with a fixed value.

Values ​​such as 1.5, 4, true, and ‘\ u0050’ that appear directly in the program without calculating 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, -5, “a”, and true indicate a constant value.

Correct

// 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;
// decimal

int decNumber = 34;

// 0x represents hexadecimal

int hexNumber = 0x2F;

// 0 represents binary

int binNumber = 0b10010;

Literals floating point

  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

  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

Exit mobile version