JAVA Fundamentals
CONSTANTS & VARIABLE
A Constant is a quantity that doesn’t change. This quantity can be stored at a
Locations in the memory of the computer.
A Variable can be considered as a name given to the location in memory
where this constant is stored.
x=3
where x is variable
3 is constant
Basic DATA/values/constant/literals:
TYPE Example Integer -128 , +127, -32767, 89, 23, +34 , 90, 0 Floating 45.89111 , -78.333 , 45.90 , +23.890 Character
’a’, ’A’, ’z’ ,’$’, ’C’ etc.
imp=> character constant is either
single alphabet, digit or a single
special symbol enclosed within single
inverted commas Boolean
True /False
String “any length of alphanumeric characters”,
“god is great” , “ Lal Bagh 123/11”
RULES FOR COSTRUCTING VARIABLE NAME :
A variable name is any combination of alphabet, digits or underscore. Or either alphabet only .
The first character in the variable name must be an alphabet.
No commas or blanks are allowed within a variable name.
No special symbol other than an underscore can be used in a variable .
example :
bankemployee
bank employee
bank*employee
bank,employee
employee123
bank_employee
123employee
Basic DATA TYPES :
TYPE SYMBOL Storage Capacity Storage range Integer
byte
short
int
long
1 byte
2 byte
4 byte
8 byte
-128 to 127
-32767 to +32768
-2,147,483,648 to +2,147,483,648
-9,223,372,036,854,775,808
to -9,223,372,036,854,775,808 Floating float
double 4 byte
8 byte -3.4e38 to +3.4e38
-3.4e308 to +3.4e308 Boolean
boolean
1 bit
true /false
Character Char 2 byte ’a’, ’A’, ’z’ ,’$’, ’C’ etc. String String varies “any length of alphanumeric characters”
OPERATORS
(1) Assignment operator : ‘=’
This operator is used to assign right hand side value to a left hand side variable.
X=5 ;
Where 5 is constant(value) is assign to left side variable x.
** But 5=x is not valid statement because ‘=’ require value to its right hand side.
x+y =7 is also not valid statement
(2) Arithmetic operators
Operator Meaning +
-
/
*
% Addition
Subtraction
Division
Multiply
Modulus ***: Rules for division:
int /int =>int eg. 8/7 =>1
in/float=>float eg 8/7.0=>1.14
float/int=>float eg 8.0/7=>1.14
***: ‘ %’ operator gives remainder value
eg. 7%3 gives 1
(3) Relational operators
Operator Meaning <
>
<=
>=
==
!= Less than
Greater than
Less than equal to
greater than equal to
equal equal to
Not equal to These operators are used to compare between two values and always return result in true or false
(4) Logical Operators
These operators are used to join two or more condition/expression and give result in true or false
Operators Meaning &&
||
! AND
OR
Not
logical AND logical OR Conditions
OUTPUT Conditions
OUTPUT C1 C2 C1 C2 T
F
T
F T
F
F
F T
F
F
F T
F
T
F T
T
T
F T
T
T
F
Program structure
class classname
{
public static void main(String args[])
{
all programme statements
……………………………….
………………………………
…………………………….
………………………………
}
}
Sample program
import java.io.*;
class square
{
public static void main (String args []) throws IOException
{
int n, Sq;
InputStreamReader ir = new InputStreamReader (System.in);
BufferedReader br = new BufferedReader (ir);
System.out.print ("ENTER THE NUMBER ");
n = Integer.parseInt (br.readLine ());
Sq=n*n;
System.out.println ("Square=" + Sq);
}
}
OUTPUT STATEMENT
1. to print any message
System.out.println (“any message“);
o/p => any massage
2. to print value of variable
example :
x=7
System.out.println(x);
o/p => 7
3. to print string and value of variable
example :
x=7
System.out.println(“value of x=”+x);
o/p
value of x=7
here + is used to concatenate string and variable
TAKING INPUT FROM USER :
Object to Created :
(1) InputStreamReader Ir=new InputStreamReader (System.in);
BufferedReader Br=new BufferedReader(Ir);
for INTERGER value :
int a=Integer.parseInt (Br.readLine());
for FLOATING value :
double b=Double.parseDouble(Br.readLine());
for CHARACTER value :
char op=(char)System.in.read();
for String :
String name=Br.readLine();
CONTROL STATEMENTS SYNTAX:
1. if statement
if ( condition )
{
Execute these statements;
}
2. if-else statement
if ( condition )
{
Execute these statements;
}
else
{
If false then execute these statements;
}
3. else if
if ( condition )
Execute these statements;
else if ( condition )
Execute these statements;
else if ( condition )
Execute these statements;
else if ( condition )
Execute these statements;
……………………………
……………………………
……………………………
…………………………
else
Execute these statements;
import java.io.*;
class Bonus
{
public static void main (String args []) throws IOException
{
int age, sal;
InputStreamReader ir = new InputStreamReader (System.in);
BufferedReader br = new BufferedReader (ir);
System.out.print ("ENTER AGE ");
age = Integer.parseInt (br.readLine ());
System.out.print ("ENTER SALARY ");
sal = Integer.parseInt (br.readLine ());
if(age>25 && sal<5000)
System.out.println("Eligible for bonus”);
else
System.out.println("Not Eligible for bonus”);
}
PROBLEM 4:
Enter percetage of the student and find the division according to
percentage division
60-100 Ist
45-59 IInd
33-44 IIIrd
0-32 Fail
import java.io.*;
class Percentage
{
public static void main (String args []) throws IOException
{
int p;
InputStreamReader ir = new InputStreamReader (System.in);
BufferedReader br = new BufferedReader (ir);
System.out.print ("ENTER PERCENTAGE ");
p = Integer.parseInt (br.readLine ());
if(p>=60 && p<=100)
System.out.println("Ist Division”);
else if(p>=45 && p<60)
System.out.println("IInd Division”);
else if(p>=33 && p<45)
System.out.println("IIIrd Division”);
else
System.out.println("Fail”);
}
}
8
GAILORD SOFTWARE TECHNOLOGIES by : Er. Gagan Kumar Kanaujia
mobile : 9839458989
Description
this file is concentrated on fundamentals concepts of java. which give information about variables, constant, data types, operators, control statemnets etc.
Presentation Transcript
Your Facebook Friends on WizIQ