HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

Java Variables

Variables are containers for storing data values.

In Java, there are different types of variables, for example:

  • String - stores text, such as "Hello". String values are surrounded by double quotes

  • int - stores integers (whole numbers), without decimals, such as 123 or -123

  • float - stores floating point numbers, with decimals, such as 19.99 or -19.99

  • char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes

  • boolean - stores values with two states: true or false


String-store text

The value of a String variable can be assigned using the assignment operator (=).
String values are surrounded by double quotes.

Example
              
 public static void main(String[ ]args){
 String name = "india";
System.out.println(name);
}
}
                    

Float-

it can store a decimal number with up to 6-7 digits of precision.

The range of values that a float variable can store is 1.40129846432481707e-45 to 3.40282346638528860e+38.

Example
Public class Float{
public static void main(String[ ]args){
float num=56.34;
 system.out.println(“num1:”+num);
  }                  
 }

                    
                    

int (integer)-

store integer value, without decimals

Example
public static void main(String[ ]args){
                    
int age= 15;
System.out.println(age);
}
}
          
 

char-

A char variable in Java is a variable that stores a single character.

The value of a char variable can be assigned using the assignment operator (=).

A char value must be surrounded by single quotes, like 'A' or 'c'.

Example
    
public static void main(String[ ]args){
Char c=’a’;
system.out.println(c);
 }
 }

                    

boolean-

A boolean variable in Java is a variable that stores a Boolean value.

Boolean values can be either true or false.


Example
    
public static void main(String[ ] args){
  boolean myBoolean=false;
system.out.println(myBoolean);
 }
}