[go: up one dir, main page]

0% found this document useful (0 votes)
4 views18 pages

Programming(1) - Lab2

Java

Uploaded by

Ahmed Mohamed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views18 pages

Programming(1) - Lab2

Java

Uploaded by

Ahmed Mohamed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Introduction to

Java Programming

Variables in Java
Lab 2
Declaring (creating) Variables
To create a variable in java we follow the syntax:

Datatype variableName = value;


Example:
Variables
Variables are containers for storing data values.
In Java, there are different types of variables called data types such as:
Data Type Size Description
byte 1 byte Stores whole numbers from -128 to 127
short 2 bytes Stores whole numbers from -32,768 to 32,767
int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647
long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807. End with 'L' . long Num = 15000000000L;
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits
boolean 1 bit Stores true or false values
char 2 bytes Stores a single character/letter or ASCII values,Char values are surrounded by
single quotes
String stores text, such as "Hello". String values are surrounded by double quotes
Rules for variables names
• Names must begin with a letter
• Names can also begin with $ and _
• Names can contain letters, digits, underscores, and dollar signs
• Names cannot contain whitespace
• Names are case sensitive ("myVar" and "myvar" are different variables)
• Reserved words (like Java keywords, such as int or boolean) cannot be
used as names
• The float and double data types can store fractional numbers. Note that
you should end the value with an "f" for floats and "d" for doubles
Declaring (creating) Variables
To create a variable in java we follow the syntax:

Datatype variableName = value;


Example:
Multiple variable with the same
Datatype
Printing variables
1- Create a variable to store the "Hello World" and print it to the console.
2- Create a variable to store a character then print it to the console
Printing variables
• Create a two variables to store floating point numbers and print their
values to the console.

Output Output
Printing variables
• Create a two variables one to store true and one to store false and print
their values to the console.

Output
ASCII Code Table
ASCII Code Example
Create a three variables to store the ASCII code for the letters A, B and C
And print them using print statement.
Output
Printing variables
Create a variable for each data type and print them each on a new line using printf statement.

Datatype variableName = value;

Output
Create two variables to store number of items and the cost
of single item.
Then create a new variable to store the total cost of all
items and print using println:
• Number of items
• Cost per item Expected output
• Total cost
Solution
Swapping Values
Create two variables to store two integer.
Then swap the values of the two variables and print the
values before and after swapping using print statement.

Expected output
Swapping Solution
Swapping using printf()
Thanks!

You might also like