• A.razaq

T4- variables in java

variables in java are locations in memory that store a specific value , in another meaning they are a container that hold and store data during the execution of the java program .


types of variables :


  • String : stores text values .


  • integers : holds and stores integers (whole numbers), without decimals, such as ( 456 ) or ( -456 ).


  • Float and Double : stores real numbers ( contain decimal points), such as in( 3.4 ) , (0.0) and (–11.19).


  • Boolean : stores values with two states: true or false.


  • char : holds single characters, such as 'a' or 'A' , it can be uppercase letter or lowercase.

----------------------------------------------------------------------------------------


how can we use variables in java ?


any variable in java first must be declared with a type and a name

before we even use it, for example

lets say we want to declare an integer variable


first we declare the type and in java integers are represented by " int "


int 

then we give it a name , lets use "x" as our variable name


int x 

then if we have a known value we want to assign to the variable x

we place an equal sigh " = " then put the value after.

for example, if we want to give the variable x a value of 10

it will be like this.


int x = 10; 


and we must always end with a semicolon ;

also we must put the correct value type

meaning , we cant declare an integer variable and then assign a decimal value to it

for example


int x = 10.5; <<<< is incorrect 


because integers are without decimal so we use float or double in this case



double x = 10.5;


string variables :


same as any variable , we declare the type String and first letter of String is capital letter

then we give it a name then a value after the equal sign between a double " " quotation marks then end with a semicolon ;

String name = " john";


we can also declare a variable without a value and give it a value later on



int x ;

in this case we have created an integer variable called x without a value

we can assign a value to it later on like this


int x ; 
x=15;



5 views0 comments

Recent Posts

See All