You are on page 1of 2

Chapter (2)

Programs, Data, Variables, and Calculation


Variable
-An address of location in the computer’s memory to store different values.
-A unit of storage than can be modified during program execution.
-Variable names in Java can begin with a letter, underscore character ( _ ), or a dollar
sign($). The rest of the name can be any letters or numbers, but you cannot use blank
space. All variables in the java must have a data type.
Caution: Java is case-sensitive when it comes to variable names
Declare: datatype variablename; eg. int weight;

Declare & Initialization: datatype variablename=value; eg. int weight=10;

Assignment Statement
Eg. a=2; a=b=2;
Eg. f=6; g=f++; //g has 6 and f has 7
Eg. f=6; g=++f; //g has 7 and f has 7
Data Types
Primitive Types

boolean (1 bit, true or false)


Numeric Types
Integers Types
byte (8 bit integer:
-128 to +127)
short (16 bit integer: - 32768 to +32767)
int (32 bit integer:
-2,147,483,648 to +2,147,483,647)
long (64 bit integer:
-9,223,372,036,854,775,808 to
+9,223,372,036,854,775,807)
char (16 bit Unicode character: eg.’A’ or ‘B’)
Float-Point Types
float (32 bit floating point decimal: 1.4E-45F to 3.4E38F)
double (64 bit floating point decimal: 4.9E-324 to 1.8E308)
Reference Types
Array Type
Class Type
Interface Type

Arithmetic Operators
1. + Addition - Subtraction *
Multiplication / Division % Modules
++ Increment by 1 -- Decrement by 1

Operator Precedence
- Bracket, Of, Division, Multiplication, Addition, Subtraction

eg. int a, b, c, ans; a=6; b=3; c=5; ans = a+b*c // ans is 21

Example (1) //to assign value to Variables using assign operator(=)


public class data
{ public static void main (String[] args)
Java Programming Ch2- 1
KMD Computer Centre
{ boolean flag = true;
char Sex = ‘M'; int n = 22;
float x = 3.14159F; String na=null;
String country = new String("United States");
System.out.println ("Flag = "+flag); System.out.println ("Sex = "+Sex);
System.out.println ("n = "+n); System.out.println ("x = "+x);
System.out.println ("na = "+na); System.out.println ("Country = "+country);
}
}

Methods that return Scans Text and Numbers using Scanner class
nextBoolean(): Scans the next token of the input into a boolean value and returns that value.
nextByte() :Scans the next token of the input as a byte.
nextInt() :Scans the next token of the input as an int.
nextLong() :Scans the next token of the input as a long.
nextFloat() :Scans the next token of the input as a float.
next() :Scans the next token of the input as a word.
nextLine() :Advances this scanner past the current line and returns the input that was
skipped.

Example (2) // to input data using method of scanner class


import java.util.Scanner;
public class calculatedata
{ public static void main (String[] args)
{ int num1,num2,total,avg;
Scanner scan=new Scanner(System.in);
System.out.println ("Enter Number 1"); num1= scan.nextInt();
System.out.println ("Enter Number 2"); num2= scan.nextInt();
total=num1+num2; avg=total/2;
System.out.println ("Total="+total); System.out.println ("Average="+avg);
}
}

Casting
• Can convert a value of one type to another by using an explicit cast. Boolean variables
can never be cast. Object can be cast to another class if they are instances of that class
or any of its subclasses.
e.g String x = (String)10; // error

Example (3)
public class TestCast
{ public static void main (String[] args)
{ float x = 10.3f; int y = 3;
int i = (int)(x/y); System.out.println ("i = "+i); }
}

Assignment
1. Write a program to input student record (name, sex, mark) from keyboard and
displays student record data.

Java Programming Ch2- 2

You might also like