SlideShare a Scribd company logo
1 of 32
PROGRAMMING IN JAVA
UNIT 2
PAPER CODE 21UCS06
CONSTANTS
 Constants refers to fixed values that do not change during the execution of a
program.
 Java supports several types of constants.
REAL= FLOAT
INTEGER CONSTANTS
 An integer constant refers to a sequence of digits.
There are 3 types of integers
• Decimal integer
• Octal integer
• Hexadecimal integer
Decimal integer:
 Decimal integers consist of a set of digits, 0 through 9, preceded by an optional minus sign.
 Embedded spaces, commas, and non-digit characters are not permitted between digits.
Ex: 123 -125 85214
52 58 20.585 $3655
Octal integer:
An octal integer constant consists of any combination of digits from set 0 through 7, with a
leading 0.
Ex:
069 025
(2*6) +(2*4) =20
Hexadecimal integer :
 A sequence of digits preceded by 0x or 0X is considered as hexadecimal integer.
 A letter A through F represents the numbers 10 through 15.
Ex: 0X2 0X9F 0xbcd 0x
Real Constants:
 Numbers containing fractional parts are called real or floating point constants.
Ex: 0.087 -0.98 456.78
 The mantissa is either a real number expressed in decimal notation or an integer.
ex: 0.65E4 12eE2 1.5E+5
4.50000000000000000000 =45E
A floating point constant may comprise four parts:
 a whole number (2,5,6,8)
 a decimal point (2.5998589)
 a fractional part (9/5)
 an exponent (2542)
 A string constant is sequence of characters enclosed between double quotes.
 The characters may be alphabets, digits, special characters and blank spaces.
Examples are: “Hello” “1997”
public class Main {
public static void main(String[] args)
{
String name = “Hello";
System.out.println(name);
}
}
SINGLE CHARACTER CONSTANTS
 A single character constant contains a single character enclosed within a pair
of single quote marks.
Ex: ‘g’ ‘5’ ‘:’ ‘+’
STRING CONSTANTS
Backslash character constants :
 Java supports some special backslash character constants that are used in output
methods.
VARIABLES
 A variable is an identifier that denotes a storage location used to store a data value.
 Variable names may consist of alphabets, digits, the underscore( _ ) and dollar characters, subject to
the following conditions:
1. They must not begin with a digit.
2. Uppercase and lowercase are distinct.
3. It should not be a keyword.
4. White space is not allowed.
5. Variable names can be of any length.
Declaration of variables :
 The declaration statement defines the type of variable.
 Variables are separated by commas.
 A declaration statement must end with semicolon.
Some valid declarations are:
int count;
float x, y;
Giving values to variables:
1. By using an assignment statement
2. By using a read statement
 A simple method of giving value to a variable is through the assignment statement as follows:
variablename = value;
hello=10;
hello=“ten”;
type variablename = value;
Int hello=10;
String hello=“ten”;
Read Statement :
 We may also give values to variables interactively through the keyword using the readline( ) method.
 The readline( ) method reads the input from the keyboard as a string which is then converted to the corresponding data type
using the data type wrapper classes.
DATA TYPES
 Data types specify the size and type of values that can be stored.
 Every variable in java has a data type.
 The variety of data types available allow the programmer to select the type appropriate to the needs of the
application.
INTEGER TYPES :
 Integer types can hold whole numbers such as 123, -96, 5678.
 Java supports four types of integers.
 They are byte, short, int, and long.
FLOATING POINT TYPES:
 Floating point type contains fractional parts such as 26.78 and -7.890.
 The float type values are single-precision numbers while the double types represent double precision
numbers.
CHARACTER TYPE :
 Java provides a character data type called char.
 The char type assumes a size of 2 bytes but, basically, it can hold only a single character.
BOOLEAN TYPE :
 It is used to test a particular condition during the execution of the program.
 There are only two values that a boolean type can take: true or false.
 Boolean type is denoted by the keyword boolean and uses only one bit of storage.
SCOPE OF VARIABLES
Java variables are actually classified into three types:
Instance variables
 Instance and class variable are declared inside a class. Instance variables are created when the objects are
instantiated and they are associated with the objects.
Class variables
 Class variables are global to a class and belong to the entire set of objects that class creates. Only one memory
location is created for each class variable.
Local variables
 Variables declared and used inside methods are called local variables. They are not available for use outside
method definition.
TYPE CASTING
 We often encounter situations where there is a need to store a value on one type into a variable of another type.
 The process of converting one data type to another is called casting.
The syntax is :
type variable1 = (type) variable2;
 Four integer types can be cast to any other type except Boolean.
 Casting into a smaller type may result in loss of data.
public class Main {
public static void main(String[] args) {
int java = 20;
double javaDouble = java;
System.out.println(java);
System.out.println(javaDouble);
}
}
Out put :
20
20.0
OPERATORS AND EXPRESSIONS
 Java supports a rich set of operators.
 An operator is a symbol that is used for manipulate data and variables.
 Operators are used in programs to manipulate data and variables.
OPERATORS ARE CLASSIFIED INTO NUMBER OF CATEGORIES:
 Arithmetic operators
 Relational operators
 Logical operators
 Assignment operators
 Increment and decrement operators
 Conditional operators
 Bitwise operators
 Special operators
ARITHMETIC OPERATORS :
• Arithmetic operators are used to construct mathematical expressions as in algebra.
Ex: a-b a+b a*b a/b a%b
• Here a and b may be variable or constants.
• They are also known as operands -, +, *, /, % are called operators.
import java.io.*;
class Addition {
public static void main(String[] args)
{
int num1 = 10, num2 = 20, sum = 0;
System.out.println("num1 = " + num1);
System.out.println("num2 = " + num2);
// adding num1 and num2
sum = num1 + num2;
System.out.println("The sum = " + sum);
}
}
Output:
num1 = 10
num2 = 20
The sum = 30
class floatpoint
{
public static void main(String args[])
{
float a=20.5F, b=6.4F;
System.out.println(" a=" +a);
System.out.println(" b=" +b );
System.out.println(" a+b= " +( a-b));
System.out.println(" a-b= " +( a-b));
System.out.println(" a*b= " +( a*b));
System.out.println(" a/b= " +( a/b));
System.out.println(" a%b= " +( a%b));
}
}
Output:
a=20.5
b=6.4
a+b= 26.9
a-b= 14.1
a*b= 131.2
a/b= 3.203125
a%b= 1.2999997
RELATIONAL OPERATORS :
 Compares two quantities depending on their relation.
 Java supports six relational operators
 Eg: Expression
value
4.5<=10 True
4.5>=10 False
class RelationalOperators
{
public static void main(String args[])
{
float a = 15.0F, b = 20.75F, c = 15.0F;
System.out.println(" a = "+ a);
System.out.println(" b = "+ b);
System.out.println(" c = "+ c);
System.out.println(" a < b is " + (a<b));
System.out.println(" a > b is " + (a>b));
System.out.println(" a == c is " + (a==c));
System.out.println(" a <= c is "+ (a<=c));
System.out.println(" a >= b is " + (a>=b));
System.out.println(" b != c is " + (b!=c));
System.out.println(" b == a+c is " + (b==a+c));
}}
Output:
a = 15.0
b = 20.75
c = 15.0
a < b is true
a > b is false
a == c is true
a <= c is true
a >= b is false
b != c is true
b == a+c is false
LOGICAL OPERATORS :
 Java has three logical operators.
 A logical operator returns either TRUE or FALSE values.
 Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT
 When an expression combines two or more relational
expressions then it is called logical expression or a compound
relational expression
Eg: if (age>55 && salary40 && mark2 >40)
INCREMENT AND DECREMENT OPERATORS :
 They are also called unary operator. ++
 Increment operator, add 1 to the operand –
 Decrement operator, subtract 1 to the operand
 They may also used to increment subscripted variables
Eg. a[i++]
class Incrementoperator
{
public static void main(String args[ ])
{
int m=10,n=20;
System.out. println("m=" +m);
System.out. println("n="+n);
System.out. println("++m="+++m);
System.out.println("n++="+n++);
Systern.out. println("m="+m);
System .out. println("n="+n); }
}
Output
m=10
n=20
++m=11
n++=21
m=11
n=21
DECISION MAKING AND BRANCHING
 When a program breaks the sequential flow and jumps to another part of the code, it is called
branching.
 When the branching is based on a particular condition, it is known as conditional branching.
 If branching takes place without any decision, it is known as unconditional branching.
The following statements are known as control or decision making statements.
 if statement
 switch statement
 Conditional operator statement
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) It is one of the most popular programming language in the world
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) It is one of the most popular programming language in the world
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) It is one of the most popular programming language in the world
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) It is one of the most popular programming language in the world
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) It is one of the most popular programming language in the world
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) It is one of the most popular programming language in the world
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) It is one of the most popular programming language in the world

More Related Content

Similar to Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) It is one of the most popular programming language in the world

Similar to Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) It is one of the most popular programming language in the world (20)

Basic_Java_02.pptx
Basic_Java_02.pptxBasic_Java_02.pptx
Basic_Java_02.pptx
 
Chapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B KuteChapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B Kute
 
MODULE_2_Operators.pptx
MODULE_2_Operators.pptxMODULE_2_Operators.pptx
MODULE_2_Operators.pptx
 
Lecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops conceptLecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops concept
 
vb.net.pdf
vb.net.pdfvb.net.pdf
vb.net.pdf
 
Presentation 2
Presentation 2Presentation 2
Presentation 2
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
Ap Power Point Chpt2
Ap Power Point Chpt2Ap Power Point Chpt2
Ap Power Point Chpt2
 
JAVASCRIPT - LinkedIn
JAVASCRIPT - LinkedInJAVASCRIPT - LinkedIn
JAVASCRIPT - LinkedIn
 
Fundamentals of C Programming Language
Fundamentals of C Programming LanguageFundamentals of C Programming Language
Fundamentals of C Programming Language
 
Unit 2.5
Unit 2.5Unit 2.5
Unit 2.5
 
Unit 2.5
Unit 2.5Unit 2.5
Unit 2.5
 
1 Revision Tour
1 Revision Tour1 Revision Tour
1 Revision Tour
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
C sharp part 001
C sharp part 001C sharp part 001
C sharp part 001
 
Data types and operators in vb
Data types and operators  in vbData types and operators  in vb
Data types and operators in vb
 
unit 1 cpds.pptx
unit 1 cpds.pptxunit 1 cpds.pptx
unit 1 cpds.pptx
 
Java chapter 2
Java chapter 2Java chapter 2
Java chapter 2
 
Getting Started with C++
Getting Started with C++Getting Started with C++
Getting Started with C++
 
Computer programming - variables constants operators expressions and statements
Computer programming - variables constants operators expressions and statementsComputer programming - variables constants operators expressions and statements
Computer programming - variables constants operators expressions and statements
 

Recently uploaded

Botany krishna series 2nd semester Only Mcq type questions
Botany krishna series 2nd semester Only Mcq type questionsBotany krishna series 2nd semester Only Mcq type questions
Botany krishna series 2nd semester Only Mcq type questionsSumit Kumar yadav
 
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...Lokesh Kothari
 
GBSN - Microbiology (Unit 2)
GBSN - Microbiology (Unit 2)GBSN - Microbiology (Unit 2)
GBSN - Microbiology (Unit 2)Areesha Ahmad
 
Botany 4th semester series (krishna).pdf
Botany 4th semester series (krishna).pdfBotany 4th semester series (krishna).pdf
Botany 4th semester series (krishna).pdfSumit Kumar yadav
 
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptxUnlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptxanandsmhk
 
Physiochemical properties of nanomaterials and its nanotoxicity.pptx
Physiochemical properties of nanomaterials and its nanotoxicity.pptxPhysiochemical properties of nanomaterials and its nanotoxicity.pptx
Physiochemical properties of nanomaterials and its nanotoxicity.pptxAArockiyaNisha
 
Zoology 4th semester series (krishna).pdf
Zoology 4th semester series (krishna).pdfZoology 4th semester series (krishna).pdf
Zoology 4th semester series (krishna).pdfSumit Kumar yadav
 
Chemistry 4th semester series (krishna).pdf
Chemistry 4th semester series (krishna).pdfChemistry 4th semester series (krishna).pdf
Chemistry 4th semester series (krishna).pdfSumit Kumar yadav
 
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43bNightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43bSérgio Sacani
 
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Sérgio Sacani
 
VIRUSES structure and classification ppt by Dr.Prince C P
VIRUSES structure and classification ppt by Dr.Prince C PVIRUSES structure and classification ppt by Dr.Prince C P
VIRUSES structure and classification ppt by Dr.Prince C PPRINCE C P
 
Green chemistry and Sustainable development.pptx
Green chemistry  and Sustainable development.pptxGreen chemistry  and Sustainable development.pptx
Green chemistry and Sustainable development.pptxRajatChauhan518211
 
Pests of mustard_Identification_Management_Dr.UPR.pdf
Pests of mustard_Identification_Management_Dr.UPR.pdfPests of mustard_Identification_Management_Dr.UPR.pdf
Pests of mustard_Identification_Management_Dr.UPR.pdfPirithiRaju
 
Chromatin Structure | EUCHROMATIN | HETEROCHROMATIN
Chromatin Structure | EUCHROMATIN | HETEROCHROMATINChromatin Structure | EUCHROMATIN | HETEROCHROMATIN
Chromatin Structure | EUCHROMATIN | HETEROCHROMATINsankalpkumarsahoo174
 
TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...
TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...
TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...ssifa0344
 
Recombinant DNA technology (Immunological screening)
Recombinant DNA technology (Immunological screening)Recombinant DNA technology (Immunological screening)
Recombinant DNA technology (Immunological screening)PraveenaKalaiselvan1
 
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 60009654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000Sapana Sha
 
Disentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOSTDisentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOSTSérgio Sacani
 
fundamental of entomology all in one topics of entomology
fundamental of entomology all in one topics of entomologyfundamental of entomology all in one topics of entomology
fundamental of entomology all in one topics of entomologyDrAnita Sharma
 

Recently uploaded (20)

Botany krishna series 2nd semester Only Mcq type questions
Botany krishna series 2nd semester Only Mcq type questionsBotany krishna series 2nd semester Only Mcq type questions
Botany krishna series 2nd semester Only Mcq type questions
 
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
 
GBSN - Microbiology (Unit 2)
GBSN - Microbiology (Unit 2)GBSN - Microbiology (Unit 2)
GBSN - Microbiology (Unit 2)
 
Botany 4th semester series (krishna).pdf
Botany 4th semester series (krishna).pdfBotany 4th semester series (krishna).pdf
Botany 4th semester series (krishna).pdf
 
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptxUnlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptx
 
Physiochemical properties of nanomaterials and its nanotoxicity.pptx
Physiochemical properties of nanomaterials and its nanotoxicity.pptxPhysiochemical properties of nanomaterials and its nanotoxicity.pptx
Physiochemical properties of nanomaterials and its nanotoxicity.pptx
 
Zoology 4th semester series (krishna).pdf
Zoology 4th semester series (krishna).pdfZoology 4th semester series (krishna).pdf
Zoology 4th semester series (krishna).pdf
 
Chemistry 4th semester series (krishna).pdf
Chemistry 4th semester series (krishna).pdfChemistry 4th semester series (krishna).pdf
Chemistry 4th semester series (krishna).pdf
 
CELL -Structural and Functional unit of life.pdf
CELL -Structural and Functional unit of life.pdfCELL -Structural and Functional unit of life.pdf
CELL -Structural and Functional unit of life.pdf
 
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43bNightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
 
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
 
VIRUSES structure and classification ppt by Dr.Prince C P
VIRUSES structure and classification ppt by Dr.Prince C PVIRUSES structure and classification ppt by Dr.Prince C P
VIRUSES structure and classification ppt by Dr.Prince C P
 
Green chemistry and Sustainable development.pptx
Green chemistry  and Sustainable development.pptxGreen chemistry  and Sustainable development.pptx
Green chemistry and Sustainable development.pptx
 
Pests of mustard_Identification_Management_Dr.UPR.pdf
Pests of mustard_Identification_Management_Dr.UPR.pdfPests of mustard_Identification_Management_Dr.UPR.pdf
Pests of mustard_Identification_Management_Dr.UPR.pdf
 
Chromatin Structure | EUCHROMATIN | HETEROCHROMATIN
Chromatin Structure | EUCHROMATIN | HETEROCHROMATINChromatin Structure | EUCHROMATIN | HETEROCHROMATIN
Chromatin Structure | EUCHROMATIN | HETEROCHROMATIN
 
TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...
TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...
TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...
 
Recombinant DNA technology (Immunological screening)
Recombinant DNA technology (Immunological screening)Recombinant DNA technology (Immunological screening)
Recombinant DNA technology (Immunological screening)
 
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 60009654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000
 
Disentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOSTDisentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOST
 
fundamental of entomology all in one topics of entomology
fundamental of entomology all in one topics of entomologyfundamental of entomology all in one topics of entomology
fundamental of entomology all in one topics of entomology
 

Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) It is one of the most popular programming language in the world

  • 1. PROGRAMMING IN JAVA UNIT 2 PAPER CODE 21UCS06
  • 2. CONSTANTS  Constants refers to fixed values that do not change during the execution of a program.  Java supports several types of constants. REAL= FLOAT
  • 3. INTEGER CONSTANTS  An integer constant refers to a sequence of digits. There are 3 types of integers • Decimal integer • Octal integer • Hexadecimal integer Decimal integer:  Decimal integers consist of a set of digits, 0 through 9, preceded by an optional minus sign.  Embedded spaces, commas, and non-digit characters are not permitted between digits. Ex: 123 -125 85214 52 58 20.585 $3655
  • 4. Octal integer: An octal integer constant consists of any combination of digits from set 0 through 7, with a leading 0. Ex: 069 025 (2*6) +(2*4) =20 Hexadecimal integer :  A sequence of digits preceded by 0x or 0X is considered as hexadecimal integer.  A letter A through F represents the numbers 10 through 15. Ex: 0X2 0X9F 0xbcd 0x
  • 5. Real Constants:  Numbers containing fractional parts are called real or floating point constants. Ex: 0.087 -0.98 456.78  The mantissa is either a real number expressed in decimal notation or an integer. ex: 0.65E4 12eE2 1.5E+5 4.50000000000000000000 =45E A floating point constant may comprise four parts:  a whole number (2,5,6,8)  a decimal point (2.5998589)  a fractional part (9/5)  an exponent (2542)
  • 6.  A string constant is sequence of characters enclosed between double quotes.  The characters may be alphabets, digits, special characters and blank spaces. Examples are: “Hello” “1997” public class Main { public static void main(String[] args) { String name = “Hello"; System.out.println(name); } } SINGLE CHARACTER CONSTANTS  A single character constant contains a single character enclosed within a pair of single quote marks. Ex: ‘g’ ‘5’ ‘:’ ‘+’ STRING CONSTANTS
  • 7. Backslash character constants :  Java supports some special backslash character constants that are used in output methods.
  • 8. VARIABLES  A variable is an identifier that denotes a storage location used to store a data value.  Variable names may consist of alphabets, digits, the underscore( _ ) and dollar characters, subject to the following conditions: 1. They must not begin with a digit. 2. Uppercase and lowercase are distinct. 3. It should not be a keyword. 4. White space is not allowed. 5. Variable names can be of any length. Declaration of variables :  The declaration statement defines the type of variable.  Variables are separated by commas.  A declaration statement must end with semicolon. Some valid declarations are: int count; float x, y;
  • 9. Giving values to variables: 1. By using an assignment statement 2. By using a read statement  A simple method of giving value to a variable is through the assignment statement as follows: variablename = value; hello=10; hello=“ten”; type variablename = value; Int hello=10; String hello=“ten”; Read Statement :  We may also give values to variables interactively through the keyword using the readline( ) method.  The readline( ) method reads the input from the keyboard as a string which is then converted to the corresponding data type using the data type wrapper classes.
  • 10. DATA TYPES  Data types specify the size and type of values that can be stored.  Every variable in java has a data type.  The variety of data types available allow the programmer to select the type appropriate to the needs of the application.
  • 11. INTEGER TYPES :  Integer types can hold whole numbers such as 123, -96, 5678.  Java supports four types of integers.  They are byte, short, int, and long. FLOATING POINT TYPES:  Floating point type contains fractional parts such as 26.78 and -7.890.  The float type values are single-precision numbers while the double types represent double precision numbers.
  • 12. CHARACTER TYPE :  Java provides a character data type called char.  The char type assumes a size of 2 bytes but, basically, it can hold only a single character. BOOLEAN TYPE :  It is used to test a particular condition during the execution of the program.  There are only two values that a boolean type can take: true or false.  Boolean type is denoted by the keyword boolean and uses only one bit of storage. SCOPE OF VARIABLES Java variables are actually classified into three types: Instance variables  Instance and class variable are declared inside a class. Instance variables are created when the objects are instantiated and they are associated with the objects. Class variables  Class variables are global to a class and belong to the entire set of objects that class creates. Only one memory location is created for each class variable. Local variables  Variables declared and used inside methods are called local variables. They are not available for use outside method definition.
  • 13. TYPE CASTING  We often encounter situations where there is a need to store a value on one type into a variable of another type.  The process of converting one data type to another is called casting. The syntax is : type variable1 = (type) variable2;  Four integer types can be cast to any other type except Boolean.  Casting into a smaller type may result in loss of data. public class Main { public static void main(String[] args) { int java = 20; double javaDouble = java; System.out.println(java); System.out.println(javaDouble); } } Out put : 20 20.0
  • 14. OPERATORS AND EXPRESSIONS  Java supports a rich set of operators.  An operator is a symbol that is used for manipulate data and variables.  Operators are used in programs to manipulate data and variables. OPERATORS ARE CLASSIFIED INTO NUMBER OF CATEGORIES:  Arithmetic operators  Relational operators  Logical operators  Assignment operators  Increment and decrement operators  Conditional operators  Bitwise operators  Special operators
  • 15. ARITHMETIC OPERATORS : • Arithmetic operators are used to construct mathematical expressions as in algebra. Ex: a-b a+b a*b a/b a%b • Here a and b may be variable or constants. • They are also known as operands -, +, *, /, % are called operators.
  • 16. import java.io.*; class Addition { public static void main(String[] args) { int num1 = 10, num2 = 20, sum = 0; System.out.println("num1 = " + num1); System.out.println("num2 = " + num2); // adding num1 and num2 sum = num1 + num2; System.out.println("The sum = " + sum); } } Output: num1 = 10 num2 = 20 The sum = 30 class floatpoint { public static void main(String args[]) { float a=20.5F, b=6.4F; System.out.println(" a=" +a); System.out.println(" b=" +b ); System.out.println(" a+b= " +( a-b)); System.out.println(" a-b= " +( a-b)); System.out.println(" a*b= " +( a*b)); System.out.println(" a/b= " +( a/b)); System.out.println(" a%b= " +( a%b)); } } Output: a=20.5 b=6.4 a+b= 26.9 a-b= 14.1 a*b= 131.2 a/b= 3.203125 a%b= 1.2999997
  • 17. RELATIONAL OPERATORS :  Compares two quantities depending on their relation.  Java supports six relational operators  Eg: Expression value 4.5<=10 True 4.5>=10 False
  • 18. class RelationalOperators { public static void main(String args[]) { float a = 15.0F, b = 20.75F, c = 15.0F; System.out.println(" a = "+ a); System.out.println(" b = "+ b); System.out.println(" c = "+ c); System.out.println(" a < b is " + (a<b)); System.out.println(" a > b is " + (a>b)); System.out.println(" a == c is " + (a==c)); System.out.println(" a <= c is "+ (a<=c)); System.out.println(" a >= b is " + (a>=b)); System.out.println(" b != c is " + (b!=c)); System.out.println(" b == a+c is " + (b==a+c)); }} Output: a = 15.0 b = 20.75 c = 15.0 a < b is true a > b is false a == c is true a <= c is true a >= b is false b != c is true b == a+c is false
  • 19. LOGICAL OPERATORS :  Java has three logical operators.  A logical operator returns either TRUE or FALSE values.  Operator Meaning && Logical AND || Logical OR ! Logical NOT  When an expression combines two or more relational expressions then it is called logical expression or a compound relational expression Eg: if (age>55 && salary40 && mark2 >40)
  • 20.
  • 21. INCREMENT AND DECREMENT OPERATORS :  They are also called unary operator. ++  Increment operator, add 1 to the operand –  Decrement operator, subtract 1 to the operand  They may also used to increment subscripted variables Eg. a[i++] class Incrementoperator { public static void main(String args[ ]) { int m=10,n=20; System.out. println("m=" +m); System.out. println("n="+n); System.out. println("++m="+++m); System.out.println("n++="+n++); Systern.out. println("m="+m); System .out. println("n="+n); } } Output m=10 n=20 ++m=11 n++=21 m=11 n=21
  • 22.
  • 23.
  • 24.
  • 25. DECISION MAKING AND BRANCHING  When a program breaks the sequential flow and jumps to another part of the code, it is called branching.  When the branching is based on a particular condition, it is known as conditional branching.  If branching takes place without any decision, it is known as unconditional branching. The following statements are known as control or decision making statements.  if statement  switch statement  Conditional operator statement