First Java Program
Thursday, July 13, 2023
7:41 PM
JAVA program:
Program:
Set of instructions.
Class :Class is collection object.
-Java Is case sensitive.
-Keyword: These reserved words.
-These they are having own meaning, you can not use.
Syntax:
class class_name
{// start of class
}//end of class
class Demo
{
//body of class
}
*************************************************************
Symbols:
{ } :---It indicates scope of block
( ):- It indicate method.
[ ] :- It indicated array.
, - to separate variable.(seperator)
. -To access properties inside a class with the help of object.
; -It indicates end of statement.(terminator)
*************************************************************
Naming convention:
[Link] name always starts with capital letter.
DemoProgram,Demo_Program,
[Link] should be written in lowercase
[Link] name should be written in camelcase.
firstName()
[Link] always write in upper case.
ID
Method:
void :nothing
return_type method_name (parameters,parameters)
{
Java-14-7 Page 1
{
// body of method
********************************************************
Parameters Explanations:
[Link]: It is keyword which is used to declare a class in java.
[Link]: It is keyword which is used to show visibility to all, accessibility by all.
[Link]:It is a keyword. There is no need to create an object of a class. The main method is
executed by the JVM no need to create object.
[Link]:It indicates return type of method. It means it doesn't return any value.
[Link]:method name which represents starting point of the program.
[Link] args[]:It is parameter accepted by main method.
[Link].:It is predefined class in java.
[Link]:object to call println().
[Link]() :This method is used to print the output and moves cursor to next line.
[Link]() :This method is used to print the output but cursor will stay on same line.
*************************************************************
Variable:
What is variable?
-Variable is a container which contains/stores value .
-Varible means whose value can varies.
-Varible value is not fixed it can change.
-A variable can strore only one value.
• Variable Declaration:
data_type variable_name ;
int x;
• variable declaration with intialization:
Data_type variable_name =initial_value;
int x=4;
Assignment:
[Link] a program to find area of Rectangle.
[Link] a program to calculate percentage of student.(6 subjects).
[Link] a program to find area cylinder.
[Link] a program to find total salary of employee per annum .
[Link] a program to perform multiplication, division of 2 numbers.
Data types in java:
[Link] data types :int,float,double,char
-basic data types.
Java-14-7 Page 2
-basic data types.
-java is a statically-typed programming languge.
-All variables must be declared before they use.
[Link]-primitive data types:Classes,Arrays,String
Data type Default size
boolean 1 bit
char 2 byte
byte 1 byte
short 2 byte
int 4 bytes
long 8 bytes
float 4 byte
double 8 bytes
** float :3.4f
** double:3.4
** long :9097865343l
=================================================================================
======
Operators in Java:
These are the symbols that is used to perform operations.
They are many types:
[Link] operator:
[Link] Operator
[Link] Operator
[Link] Operator
[Link] operator
[Link] Operator
[Link] operator
[Link] Operator.
** Unary Operator:
This operator it used single operand.
Types:
[Link]--------------increase value by one
a. prefix increment operator:
syntax: ++x;
-increment value in memory then use
b. postfix increment operator:
syntax: x++;
-Use first then increment in memory.
[Link]------------decrease value by one.
[Link]:
Syntax: --x
Java-14-7 Page 3
Syntax: --x
decrease first then use
[Link]:
Syntax: x--
Use first then decrease.
3) ! :-You can use this operator with boolean data type.
Boolean b=false;
!b=true;
-this operator returns opposite value.
[Link] Arithmetic Operators:
Arithemetic operators are used to perform basic arithmetic operation.
Addition(+);
Substraction(-)
Multiplication(*)
Division(/)
Modulus(%)
Operartor precendence: multiplicative: [* / %]
Additive:[+ -]
[Link] Operators:
[Link] shift: (<<)
- In java left shift operator is used to shift all of the bits in a values to the left side of specified
number of times.
-Embedded system.
-Internally they works on binary values.
-Digital logic.
Decimal--binary--decimal
-Example : (14<<4) :14 left shift by 4
-14 *2 ^4
(15<<3)
-15 *2 ^3
[Link] shift:(>>)
It used to move bits to the right side by the number of bits specified.
(10>>2)
-10/2^2
[Link] Operators:
-These operator are used to compare between operands and returns boolean value as a result.
Java-14-7 Page 4
-These operator are used to compare between operands and returns boolean value as a result.
-Mainly these operator are used in control statement.
>
<
<=
>=
== :Equality
!
[Link] Operators:
[Link] (&&)
[Link](||):-----similar +
-AND(&&)-----similar
This operator returns true as *. if either of condition is true .
This
It will operator
returnsreturns
false iftrue
bothif the
onlyconditions
both conditions are true.
are false.
If
True any:1condition is false it will return false.
and False:0
-True(1) and false (0)
condition1
Condition1 conditioncondition ResulResult
TT (1) TT (1) TT(1*1)
T(1)
T F F(0) TF(1*0)
FF TT TF
FF FF FF
[Link] Not(!):Opposite value
False=true
True=false
[Link] (^):
This operator returns true if either statements one is true or statement two is true but not
both.
Condition1 condition2 Resul
T T F
T F T
F T T
F F F
Bitwise Operators:
[Link] AND (&):
condition1 condition Resul
1T 1 T 1
Java-14-7 Page 5
1 T 0 F 0
0 F 1 T 0
0 F 0F 0
[Link] OR (|) :
Condition1 condition Resul
T T T
T F T
F T T
F F F
**Difference between logical and bitwise AND:
&& :If first condition is false then it will check not second condition.
If first condition is true then it will check second condition.
&: If first condition is either true or false it will always check for second condition.
Java-14-7 Page 6