Java Basics
C Manasa
Asst. professor
Dept. of CSE
Contents
• Data Types
• Operators
• Control Structures & Loops
• Arrays
Primitive Data Types
• Primitive data types are built into the Java language
and are not derived from classes.
• There are 8 Java primitive data types.
• byte • float
• short • double
• int • boolean
• long • char
Numeric Data Types
byte 1 byte Integers in the range
-128 to +127
short 2 bytes Integers in the range of
-32,768 to +32,767
int 4 bytes Integers in the range of
-2,147,483,648 to +2,147,483,647
long 8 bytes Integers in the range of
-9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
float 4 bytes Floating-point numbers in the range of
±3.410-38 to ±3.41038, with 7 digits of accuracy
double 8 bytes Floating-point numbers in the range of
±1.710-308 to ±1.710308, with 15 digits of accuracy
Example
Write a program to declare and initialize name, id and salary of an employee and print it.
The Scanner Class
• To read input from the keyboard we can use the
Scanner class.
• The Scanner class is defined in [Link], so we
will use the following statement at the top of our
programs:
import [Link];
The Scanner Class
The package of the Scanner class – [Link], needs to be imported to
use the Scanner class.
• import [Link];
Once the import of the package is done, the next step is to create the object
of the Scanner class. Steps to create the Scanner objects are as follow:
• Scanner obj1 = new Scanner([Link]);
Use Scanner functions that help take input from the user along with its
specific data type.
• See example: [Link]
The Scanner Class
The Scanner Class
Operators
Arithmetic
Logical
Relational
Conditional
Increment and Decrement
Bitwise
Control Structures
The if Statement
The if-else Statement
Nested if statements
The if-else-if Statement
The switch Statement
Example
Write a program to read from month from user and
display season.
Month 12,1,2:Winter
Month 3,4,5:Summer
Month 6,7,8: Monsoon
Month 9,10,11:Autum
Loops
For
While
Do while
The Enhanced for Loop
• Simplified array processing (read only)
• Always goes through all elements
• General format:
for(datatype elementVariable : array)
statement;
Example:
int[] numbers = {3, 6, 9};
for(int val : numbers)
{
[Link]("The next
value is " +
val);
}
Creating Arrays
• An array is an object so it needs an object reference.
// Declare a reference to an array that will hold integers.
int[] numbers;
• The next step creates the array and assigns its address to the
numbers variable.
// Create a new array that will hold 6 integers.
numbers = new int[6];
0 0 0 0 0 0
index 0 index 1 index 2 index 3 index 4 index 5
Array element values are initialized to 0.
Array indexes always start at 0.
Creating Arrays
• It is possible to declare an array reference and create
it in the same statement.
int[] numbers = new int[6];
• Arrays may be of any type.
float[] temperatures = new float[100];
char[] letters = new char[41];
long[] units = new long[50];
double[] sizes = new double[1200];
Array Initialization
• When relatively few items need to be initialized, an
initialization list can be used to initialize the array.
int[]days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
• The numbers in the list are stored in the array in order:
• days[0] is assigned 31,
• days[1] is assigned 28,
• days[2] is assigned 31,
• days[3] is assigned 30,
• etc.
• See example: [Link]
8-17
Alternate Array Declaration
•Previously we showed arrays being declared:
int[] numbers;
• However, the brackets can also go here:
int numbers[];
• These are equivalent but the first style is typical.
•Multiple arrays can be declared on the same line.
int[] numbers, codes, scores;
•With the alternate notation each variable must have brackets.
int numbers[], codes[], scores;
• The scores variable in this instance is simply an int variable.
8-18
Example
Write a program to declare and initialize 6 array elements and
find its sum and average.
8-19
Thank You.