Saturday, March 19, 2022

JAVA VARIABLES

 

Java Variables

Variables are containers for storing data values.

In Java, there are diverse varieties 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

Declaring  Variables-

To generate a variable, you should identify the type and reserve it a value:

Syntax-

type variableName = value;

  • From the above line of code type describes java variables type for example int or String furthermore variableName is the name of the variable ... for example- name or sowmii etcc....

  • the equal to sign on the right hand side shows the value reserved for the variable.......

  • for example String name = "sowmiii"; 

  • String= variable type

  • name=variable name

  • sowmii=value of the variable

To create a variable that should store text, look at the following example:

Example -1

Create a variable called text of type  of variable is String and assign it the value “Java”..

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

Output-Java

To create a variable that should store a number, look at the following example:

Example-2

Create a variable called number of type int and assign it the value 45-

public class Main {

public static void main(String[] args) {

int number= 45;

System.out.println(number);

}

}

15 Output-45

You can also announce or declare a variable without assigning the value and assign the value the later.......

Example

public class Main {

public static void main(String[] args) {

int number;

number= 45;

System.out.println(number);

}

}

Output-45

Note that if you allocate a new value to an existing variable, it will overwrite the previous value:

Example

Change the value of number from 45 to 80:

public class Main {

public static void main(String[] args) {

int number = 45;

number = 80; // number is now 80

System.out.println(number);

}

}

Output-80

Final Variables

If you don't want anyone to overwrite existing values, use the final keyword (this will announce the variable as "final" or "constant", which means permanent and read-only):

Example

public class Main {

public static void main(String[] args) {

final int number= 15;

number= 20; // will generate an error

System.out.println(number);

}

}

output: Main.java :4: error: cannot assign a value to final variable  number

number =20;

^

1 error

Other Types

A trail of how to declare variables of other types:

Example

int number= 5;
float number = 5.99f;
char number = 'D';
boolean bool= true;
String text = "Hello";

Related articles

Beginner guide for java -1

why java

Explore JDK,JVM,JRE versions

A simple pillow talk between java and java user!!!!!!!!........

write java code in a simple text editor

java setup path for windows

java syntax-1

Java Syntax Explanation

Java Declare Multiple Variables

Java Print Variables

JAVA VARIABLES

Comments in Java







No comments:

Post a Comment

Did you find this article helpful?
😁 😀

Java identifers

  Identifiers All Java variables must be identified with unique names . For example -different people have different names These unique ...