際際滷

際際滷Share a Scribd company logo
masudtarek@outlook.com
/* Our first simple Java program */
// file name: Hello.java
public class Hello
{
public static void main (String[] args)
{
System.out.println ("Hello World");
}
}
Comments
Braces indicate start
and end of main
Function to print to screen
What to print End of
statement
All Java programs have a main function;
they also start at main
2Masud Tarek
 Identifiers are names for variables, classes,
methods etc.
 Good ones are compact, but indicate what they
stand for
 Radius, StudentName, Area, MilesPerHour
 Case sensitive
 May contain upper case, lower case letters,
numbers, underscore, dollar sign
 Must not begin with a number
 Generally (convention), Class names begin with
Uppercase letter and method-names and
variable-names begin with lowercase letter
3Masud Tarek
 Some words are reserved, and cant be used as
identifiers. There are about 50 keywords in java.
public class DisplayForecast {
public static void main(String[] args) {
System.out.print(Hello, welcome to Java. ");
System.out.println(This is my first Java
program.");
}
}
4Masud Tarek
 Expression represents a single number or
character.
 It may consists of a single entity or may be
combination of different entities
interconnected by operators
 a+b
 x=1+4y
 i++
 x<=y
5Masud Tarek
 A statement causes the computer to carry out
some actions.
 There are 3 types of statements:
 Expression statement
 Consists of an expression and semicolon
 myVariable = 10 ;
 Compound statement
 Several statements within a block { }
 { R=10; Area=3.141*R*R; } // no semicolon
 Control statement
 Controls the flow of the program
 for (i=0; i<n; i++) { System.out.println(%d, i);}
 // no semicolon
6Masud Tarek
 Java is a strong typed language
 Each variable has to be declared type before use
 double x;
 x=400.23;
 There are two kinds of data-types in Java:
 Primitive types.
 Classes (will be discussed later).
7Masud Tarek
 8 primitive data types
 Integers
An 8-bit signed integer.byte
A 16-bit signed integer.short
A 32-bit signed integer.int
A 64-bit signed integer.long
8Masud Tarek
 Floating
 Others
 Also another special type: void
32-bitfloat
64-bitdouble
Either true or false.boolean
A 16-bit Unicode character.char
9Masud Tarek
 Range of data for each type
 Byte: -128 to +127
 Short: -32768 to +32767
 Integer: -2147483648 to + 2147483647
 Long : -9223372036854775808 to
+ 9223372036854775807
 Float: 1.4e045 to 3.4e+038
 Double: 4.9e-324 to 1.8e+308 (approx.)
 Boolean : true, false
 Char: 0 to 65535
 How negative data are kept
 As 2s complement of that number with sign bit=1
 Because of 2s complement there is no -0. 2s
complement of 0 is also 0 (not -0)
10Masud Tarek
 Integer:
 123 (no comma, decimal point) (decimal = start with 1
to 9, not zero)
 0b10101 binary leading zero b (digit 0 1)
 0123 octal leading with zero (digit 0 to 7)
 0x23Af3 leading zero x (digit 0 to 9, a to f)
 123_3454, 0x2___34F__45B (one or more underscore,
must start/end with digit)
 For long integer, the value should end with L
 987642L
11Masud Tarek
 Real numbers
 Float numbers have to end with F or f
 656.78F
 Numbers may have underscores (not at the
start/end)
 67__65.5_6, 0x65_6.98_67_2
 In scientific notation, for decimal E (base 10) is
used where as for hexa-decimal P(base 16) is used
 32e+8, 0x4ABp-2
12Masud Tarek
 Character
 Enclosed by single quote a b Z 2 @
 Escape Sequence (read book) used for
 Non Printable characters
 Special characters
 Unicode/Hexa/Octal
 141 octal equivalent to a
 u0061 hexa/unicode equivalent to a
 String
 Enclosed by double quote hello world
 Escape sequence () also can be used
13Masud Tarek
 In Java, there are 2 types of scope
(visibility/lifetime)
 Variables that are created inside a method have scope
within the curly braces { } they are created within.
... Mathod_one( )
{ ...
... { int x=0; // scope of x is within the inner block {}
...
}
... // this block is outside of the scope of x
... }
 Variables declared within class level have a scope within
the class
 Static variable of a class can be used outside of the class
only with a reference of the class
14Masud Tarek
 Automatic Type Conversion:
 When two types are compatible
 The destination type with larger memory size than
the source type.
 Example:
 int type is larger than byte value
 The numeric types are compatible with each other.
 The numeric types are not compatible with
character or boolean
 char and boolean are not compatible with each
other
15Masud Tarek
 (target_type) variable_name
 Example:
 int x=50;
 byte y;
 y=(byte)x;
 If target type has smaller range, reduced
modulo value will be assigned
 Suppose in the previous example, x=258
 So, y=258%(Bytes max value+1)=258%128=2
 From float to int/long, decimal part will be
truncated
16Masud Tarek
 In an expression data are automatically promoted
to higher type if one operand is in higher type
 byte-short-int-long-float-double
 Byte and short are always promoted to int
 This may lead to error of an apparently correct
coding
 byte x;
 x=50*2;
 this may give compilation error because during calculation,
result is automatically converted to int value, so without
typecasting, result can not be assigned to x
 Solution: x=(byte)(50*2);
 So, for safe, use int/long/double data types if possible
17Masud Tarek
 Chapter 2 and 3
 Review Questions:
1. Mention some rules and conventions of naming an
identifier
2. How many keywords in Java? Are followings are
keywords in Java: null, true, false (why-explain)
3. Define expression and statement.
4. How many primitive data types in Java? Mention their
memory size.
5. Discuss about integer and real number literals.
6. What will be the scope of a variable in java?
7. What is the automatic typecasting mechanism of
expression evaluation in java
18Masud Tarek

More Related Content

What's hot (20)

Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
Constructor in java
Constructor in javaConstructor in java
Constructor in java
Pavith Gunasekara
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
Vinod Kumar
Wrapper classes
Wrapper classes Wrapper classes
Wrapper classes
Kongu Engineering College, Perundurai, Erode
Constants, Variables and Data Types in Java
Constants, Variables and Data Types in JavaConstants, Variables and Data Types in Java
Constants, Variables and Data Types in Java
Abhilash Nair
CLASS & OBJECT IN JAVA
CLASS & OBJECT  IN JAVACLASS & OBJECT  IN JAVA
CLASS & OBJECT IN JAVA
Riaj Uddin Mahi
Introduction to Java Programming, Basic Structure, variables Data type, input...
Introduction to Java Programming, Basic Structure, variables Data type, input...Introduction to Java Programming, Basic Structure, variables Data type, input...
Introduction to Java Programming, Basic Structure, variables Data type, input...
Mr. Akaash
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
Alok Kumar
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
arvind pandey
Core Java Tutorials by Mahika Tutorials
Core Java Tutorials by Mahika TutorialsCore Java Tutorials by Mahika Tutorials
Core Java Tutorials by Mahika Tutorials
Mahika Tutorials
Java Data Types
Java Data TypesJava Data Types
Java Data Types
Spotle.ai
Applets in java
Applets in javaApplets in java
Applets in java
Wani Zahoor
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
Nilesh Dalvi
Java Presentation For Syntax
Java Presentation For SyntaxJava Presentation For Syntax
Java Presentation For Syntax
PravinYalameli
Oops concepts || Object Oriented Programming Concepts in Java
Oops concepts || Object Oriented Programming Concepts in JavaOops concepts || Object Oriented Programming Concepts in Java
Oops concepts || Object Oriented Programming Concepts in Java
Madishetty Prathibha
Data types, Variables, Expressions & Arithmetic Operators in java
Data types, Variables, Expressions & Arithmetic Operators in javaData types, Variables, Expressions & Arithmetic Operators in java
Data types, Variables, Expressions & Arithmetic Operators in java
Javed Rashid
Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVA
Ankita Totala
this keyword in Java.pptx
this keyword in Java.pptxthis keyword in Java.pptx
this keyword in Java.pptx
ParvizMirzayev2
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
Naz Abdalla
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
yugandhar vadlamudi
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
Vinod Kumar
Constants, Variables and Data Types in Java
Constants, Variables and Data Types in JavaConstants, Variables and Data Types in Java
Constants, Variables and Data Types in Java
Abhilash Nair
CLASS & OBJECT IN JAVA
CLASS & OBJECT  IN JAVACLASS & OBJECT  IN JAVA
CLASS & OBJECT IN JAVA
Riaj Uddin Mahi
Introduction to Java Programming, Basic Structure, variables Data type, input...
Introduction to Java Programming, Basic Structure, variables Data type, input...Introduction to Java Programming, Basic Structure, variables Data type, input...
Introduction to Java Programming, Basic Structure, variables Data type, input...
Mr. Akaash
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
Alok Kumar
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
arvind pandey
Core Java Tutorials by Mahika Tutorials
Core Java Tutorials by Mahika TutorialsCore Java Tutorials by Mahika Tutorials
Core Java Tutorials by Mahika Tutorials
Mahika Tutorials
Java Data Types
Java Data TypesJava Data Types
Java Data Types
Spotle.ai
Applets in java
Applets in javaApplets in java
Applets in java
Wani Zahoor
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
Nilesh Dalvi
Java Presentation For Syntax
Java Presentation For SyntaxJava Presentation For Syntax
Java Presentation For Syntax
PravinYalameli
Oops concepts || Object Oriented Programming Concepts in Java
Oops concepts || Object Oriented Programming Concepts in JavaOops concepts || Object Oriented Programming Concepts in Java
Oops concepts || Object Oriented Programming Concepts in Java
Madishetty Prathibha
Data types, Variables, Expressions & Arithmetic Operators in java
Data types, Variables, Expressions & Arithmetic Operators in javaData types, Variables, Expressions & Arithmetic Operators in java
Data types, Variables, Expressions & Arithmetic Operators in java
Javed Rashid
Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVA
Ankita Totala
this keyword in Java.pptx
this keyword in Java.pptxthis keyword in Java.pptx
this keyword in Java.pptx
ParvizMirzayev2
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
Naz Abdalla
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
yugandhar vadlamudi

Similar to 02 data types in java (20)

OOP-java-variables.pptx
OOP-java-variables.pptxOOP-java-variables.pptx
OOP-java-variables.pptx
ssuserb1a18d
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
Jayfee Ramos
Chapter 2 java
Chapter 2 javaChapter 2 java
Chapter 2 java
Ahmad sohail Kakar
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogram
princepavan
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogram
princepavan
Basic
BasicBasic
Basic
Shehrevar Davierwala
core java
 core java core java
core java
dssreenath
Java 17
Java 17Java 17
Java 17
Mutlu Okuducu
C language basics
C language basicsC language basics
C language basics
Nikshithas R
Java Tut1
Java Tut1Java Tut1
Java Tut1
guest5c8bd1
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
Vijay A Raj
Java tut1
Java tut1Java tut1
Java tut1
Ajmal Khan
Tutorial java
Tutorial javaTutorial java
Tutorial java
Abdul Aziz
Preparing Java 7 Certifications
Preparing Java 7 CertificationsPreparing Java 7 Certifications
Preparing Java 7 Certifications
Giacomo Veneri
JavaScript - Chapter 4 - Types and Statements
 JavaScript - Chapter 4 - Types and Statements JavaScript - Chapter 4 - Types and Statements
JavaScript - Chapter 4 - Types and Statements
WebStackAcademy
00_Introduction to Java.ppt
00_Introduction to Java.ppt00_Introduction to Java.ppt
00_Introduction to Java.ppt
HongAnhNguyn285885
Identifiers, keywords and types
Identifiers, keywords and typesIdentifiers, keywords and types
Identifiers, keywords and types
Daman Toor
Unit 1: Primitive Types - Variables and Datatypes
Unit 1: Primitive Types - Variables and DatatypesUnit 1: Primitive Types - Variables and Datatypes
Unit 1: Primitive Types - Variables and Datatypes
agautham211
2.Lesson Plan - Java Variables and Data types.pdf.pdf
2.Lesson Plan - Java Variables and Data types.pdf.pdf2.Lesson Plan - Java Variables and Data types.pdf.pdf
2.Lesson Plan - Java Variables and Data types.pdf.pdf
AbhishekSingh757567
Java tutorial PPT
Java tutorial  PPTJava tutorial  PPT
Java tutorial PPT
Intelligo Technologies
OOP-java-variables.pptx
OOP-java-variables.pptxOOP-java-variables.pptx
OOP-java-variables.pptx
ssuserb1a18d
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
Jayfee Ramos
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogram
princepavan
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogram
princepavan
C language basics
C language basicsC language basics
C language basics
Nikshithas R
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
Vijay A Raj
Tutorial java
Tutorial javaTutorial java
Tutorial java
Abdul Aziz
Preparing Java 7 Certifications
Preparing Java 7 CertificationsPreparing Java 7 Certifications
Preparing Java 7 Certifications
Giacomo Veneri
JavaScript - Chapter 4 - Types and Statements
 JavaScript - Chapter 4 - Types and Statements JavaScript - Chapter 4 - Types and Statements
JavaScript - Chapter 4 - Types and Statements
WebStackAcademy
00_Introduction to Java.ppt
00_Introduction to Java.ppt00_Introduction to Java.ppt
00_Introduction to Java.ppt
HongAnhNguyn285885
Identifiers, keywords and types
Identifiers, keywords and typesIdentifiers, keywords and types
Identifiers, keywords and types
Daman Toor
Unit 1: Primitive Types - Variables and Datatypes
Unit 1: Primitive Types - Variables and DatatypesUnit 1: Primitive Types - Variables and Datatypes
Unit 1: Primitive Types - Variables and Datatypes
agautham211
2.Lesson Plan - Java Variables and Data types.pdf.pdf
2.Lesson Plan - Java Variables and Data types.pdf.pdf2.Lesson Plan - Java Variables and Data types.pdf.pdf
2.Lesson Plan - Java Variables and Data types.pdf.pdf
AbhishekSingh757567

Recently uploaded (20)

Analysis of Conf File Parameters in Odoo 17
Analysis of Conf File Parameters in Odoo 17Analysis of Conf File Parameters in Odoo 17
Analysis of Conf File Parameters in Odoo 17
Celine George
APM London Network: Essentials of a Good PMO, 2 April 2025
APM London Network: Essentials of a Good PMO, 2 April 2025APM London Network: Essentials of a Good PMO, 2 April 2025
APM London Network: Essentials of a Good PMO, 2 April 2025
Association for Project Management
PSD-I Exam Dumps: Your Key to Passing on the First Try
PSD-I Exam Dumps: Your Key to Passing on the First TryPSD-I Exam Dumps: Your Key to Passing on the First Try
PSD-I Exam Dumps: Your Key to Passing on the First Try
lethamcmullen
Research in Physical Education by Diwakar Kashyap Sir
Research in Physical Education by Diwakar Kashyap SirResearch in Physical Education by Diwakar Kashyap Sir
Research in Physical Education by Diwakar Kashyap Sir
Diwakar Kashyap
Introduction to Drug Design.pptx by Mrs. Manjushri P. Dabhade
Introduction to Drug Design.pptx by Mrs. Manjushri P. DabhadeIntroduction to Drug Design.pptx by Mrs. Manjushri P. Dabhade
Introduction to Drug Design.pptx by Mrs. Manjushri P. Dabhade
Dabhade madam Dabhade
Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...
Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...
Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...
Sue Beckingham
Proteins, Bio similars & Antibodies.pptx
Proteins, Bio similars &  Antibodies.pptxProteins, Bio similars &  Antibodies.pptx
Proteins, Bio similars & Antibodies.pptx
Ashish Umale
Using GenAI for Universal Design for Learning
Using GenAI for Universal Design for LearningUsing GenAI for Universal Design for Learning
Using GenAI for Universal Design for Learning
Damian T. Gordon
PATENTABILITY UNDER THE 2025 CRI DRAFT GUIDELINES
PATENTABILITY UNDER THE 2025 CRI DRAFT GUIDELINESPATENTABILITY UNDER THE 2025 CRI DRAFT GUIDELINES
PATENTABILITY UNDER THE 2025 CRI DRAFT GUIDELINES
BananaIP Counsels
Mixed_Sinhala_Dual_Male_Names (1).pdf...
Mixed_Sinhala_Dual_Male_Names (1).pdf...Mixed_Sinhala_Dual_Male_Names (1).pdf...
Mixed_Sinhala_Dual_Male_Names (1).pdf...
keshanf79
Synthesis for VIth SEM 21-2-25.pptx by Mrs. Manjushri P. Dabhade
Synthesis for VIth SEM 21-2-25.pptx by Mrs. Manjushri P. DabhadeSynthesis for VIth SEM 21-2-25.pptx by Mrs. Manjushri P. Dabhade
Synthesis for VIth SEM 21-2-25.pptx by Mrs. Manjushri P. Dabhade
Dabhade madam Dabhade
nature and importance of Indian Knowledge System
nature and importance of Indian Knowledge Systemnature and importance of Indian Knowledge System
nature and importance of Indian Knowledge System
hanishabatra0
Strategic Corporate Social Responsibility: Sustainable Value Creation Fourth
Strategic Corporate Social Responsibility: Sustainable Value Creation FourthStrategic Corporate Social Responsibility: Sustainable Value Creation Fourth
Strategic Corporate Social Responsibility: Sustainable Value Creation Fourth
keileyrazawi
technology in banking ppt FOR E-CONTENT -2.ppt
technology in banking ppt  FOR E-CONTENT -2.ppttechnology in banking ppt  FOR E-CONTENT -2.ppt
technology in banking ppt FOR E-CONTENT -2.ppt
HARIHARAN A
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-6-2025 ver 5.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-6-2025 ver 5.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-6-2025 ver 5.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-6-2025 ver 5.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
MUSIC QUIZ | THE QUIZ CLUB OF PSGCAS | 12 MARCH 2025
MUSIC QUIZ | THE QUIZ CLUB OF PSGCAS | 12 MARCH 2025MUSIC QUIZ | THE QUIZ CLUB OF PSGCAS | 12 MARCH 2025
MUSIC QUIZ | THE QUIZ CLUB OF PSGCAS | 12 MARCH 2025
Quiz Club of PSG College of Arts & Science
A-Z GENERAL QUIZ | THE QUIZ CLUB OF PSGCAS | 14TH MARCH 2025.pptx
A-Z GENERAL QUIZ | THE QUIZ CLUB OF PSGCAS | 14TH MARCH 2025.pptxA-Z GENERAL QUIZ | THE QUIZ CLUB OF PSGCAS | 14TH MARCH 2025.pptx
A-Z GENERAL QUIZ | THE QUIZ CLUB OF PSGCAS | 14TH MARCH 2025.pptx
Quiz Club of PSG College of Arts & Science
IB-Unit-4 BBA BVIMR 2022 Syllabus_watermark.pdf
IB-Unit-4 BBA BVIMR 2022 Syllabus_watermark.pdfIB-Unit-4 BBA BVIMR 2022 Syllabus_watermark.pdf
IB-Unit-4 BBA BVIMR 2022 Syllabus_watermark.pdf
Dr. Mahtab Alam
McElaney "What is inclusive publishing and why do we care about accessibility...
McElaney "What is inclusive publishing and why do we care about accessibility...McElaney "What is inclusive publishing and why do we care about accessibility...
McElaney "What is inclusive publishing and why do we care about accessibility...
National Information Standards Organization (NISO)
Anti-Protozoal Agents.pptx by Mrs. Manjushri P. Dabhade
Anti-Protozoal Agents.pptx by Mrs. Manjushri P. DabhadeAnti-Protozoal Agents.pptx by Mrs. Manjushri P. Dabhade
Anti-Protozoal Agents.pptx by Mrs. Manjushri P. Dabhade
Dabhade madam Dabhade
Analysis of Conf File Parameters in Odoo 17
Analysis of Conf File Parameters in Odoo 17Analysis of Conf File Parameters in Odoo 17
Analysis of Conf File Parameters in Odoo 17
Celine George
PSD-I Exam Dumps: Your Key to Passing on the First Try
PSD-I Exam Dumps: Your Key to Passing on the First TryPSD-I Exam Dumps: Your Key to Passing on the First Try
PSD-I Exam Dumps: Your Key to Passing on the First Try
lethamcmullen
Research in Physical Education by Diwakar Kashyap Sir
Research in Physical Education by Diwakar Kashyap SirResearch in Physical Education by Diwakar Kashyap Sir
Research in Physical Education by Diwakar Kashyap Sir
Diwakar Kashyap
Introduction to Drug Design.pptx by Mrs. Manjushri P. Dabhade
Introduction to Drug Design.pptx by Mrs. Manjushri P. DabhadeIntroduction to Drug Design.pptx by Mrs. Manjushri P. Dabhade
Introduction to Drug Design.pptx by Mrs. Manjushri P. Dabhade
Dabhade madam Dabhade
Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...
Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...
Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...
Sue Beckingham
Proteins, Bio similars & Antibodies.pptx
Proteins, Bio similars &  Antibodies.pptxProteins, Bio similars &  Antibodies.pptx
Proteins, Bio similars & Antibodies.pptx
Ashish Umale
Using GenAI for Universal Design for Learning
Using GenAI for Universal Design for LearningUsing GenAI for Universal Design for Learning
Using GenAI for Universal Design for Learning
Damian T. Gordon
PATENTABILITY UNDER THE 2025 CRI DRAFT GUIDELINES
PATENTABILITY UNDER THE 2025 CRI DRAFT GUIDELINESPATENTABILITY UNDER THE 2025 CRI DRAFT GUIDELINES
PATENTABILITY UNDER THE 2025 CRI DRAFT GUIDELINES
BananaIP Counsels
Mixed_Sinhala_Dual_Male_Names (1).pdf...
Mixed_Sinhala_Dual_Male_Names (1).pdf...Mixed_Sinhala_Dual_Male_Names (1).pdf...
Mixed_Sinhala_Dual_Male_Names (1).pdf...
keshanf79
Synthesis for VIth SEM 21-2-25.pptx by Mrs. Manjushri P. Dabhade
Synthesis for VIth SEM 21-2-25.pptx by Mrs. Manjushri P. DabhadeSynthesis for VIth SEM 21-2-25.pptx by Mrs. Manjushri P. Dabhade
Synthesis for VIth SEM 21-2-25.pptx by Mrs. Manjushri P. Dabhade
Dabhade madam Dabhade
nature and importance of Indian Knowledge System
nature and importance of Indian Knowledge Systemnature and importance of Indian Knowledge System
nature and importance of Indian Knowledge System
hanishabatra0
Strategic Corporate Social Responsibility: Sustainable Value Creation Fourth
Strategic Corporate Social Responsibility: Sustainable Value Creation FourthStrategic Corporate Social Responsibility: Sustainable Value Creation Fourth
Strategic Corporate Social Responsibility: Sustainable Value Creation Fourth
keileyrazawi
technology in banking ppt FOR E-CONTENT -2.ppt
technology in banking ppt  FOR E-CONTENT -2.ppttechnology in banking ppt  FOR E-CONTENT -2.ppt
technology in banking ppt FOR E-CONTENT -2.ppt
HARIHARAN A
IB-Unit-4 BBA BVIMR 2022 Syllabus_watermark.pdf
IB-Unit-4 BBA BVIMR 2022 Syllabus_watermark.pdfIB-Unit-4 BBA BVIMR 2022 Syllabus_watermark.pdf
IB-Unit-4 BBA BVIMR 2022 Syllabus_watermark.pdf
Dr. Mahtab Alam
Anti-Protozoal Agents.pptx by Mrs. Manjushri P. Dabhade
Anti-Protozoal Agents.pptx by Mrs. Manjushri P. DabhadeAnti-Protozoal Agents.pptx by Mrs. Manjushri P. Dabhade
Anti-Protozoal Agents.pptx by Mrs. Manjushri P. Dabhade
Dabhade madam Dabhade

02 data types in java

  • 2. /* Our first simple Java program */ // file name: Hello.java public class Hello { public static void main (String[] args) { System.out.println ("Hello World"); } } Comments Braces indicate start and end of main Function to print to screen What to print End of statement All Java programs have a main function; they also start at main 2Masud Tarek
  • 3. Identifiers are names for variables, classes, methods etc. Good ones are compact, but indicate what they stand for Radius, StudentName, Area, MilesPerHour Case sensitive May contain upper case, lower case letters, numbers, underscore, dollar sign Must not begin with a number Generally (convention), Class names begin with Uppercase letter and method-names and variable-names begin with lowercase letter 3Masud Tarek
  • 4. Some words are reserved, and cant be used as identifiers. There are about 50 keywords in java. public class DisplayForecast { public static void main(String[] args) { System.out.print(Hello, welcome to Java. "); System.out.println(This is my first Java program."); } } 4Masud Tarek
  • 5. Expression represents a single number or character. It may consists of a single entity or may be combination of different entities interconnected by operators a+b x=1+4y i++ x<=y 5Masud Tarek
  • 6. A statement causes the computer to carry out some actions. There are 3 types of statements: Expression statement Consists of an expression and semicolon myVariable = 10 ; Compound statement Several statements within a block { } { R=10; Area=3.141*R*R; } // no semicolon Control statement Controls the flow of the program for (i=0; i<n; i++) { System.out.println(%d, i);} // no semicolon 6Masud Tarek
  • 7. Java is a strong typed language Each variable has to be declared type before use double x; x=400.23; There are two kinds of data-types in Java: Primitive types. Classes (will be discussed later). 7Masud Tarek
  • 8. 8 primitive data types Integers An 8-bit signed integer.byte A 16-bit signed integer.short A 32-bit signed integer.int A 64-bit signed integer.long 8Masud Tarek
  • 9. Floating Others Also another special type: void 32-bitfloat 64-bitdouble Either true or false.boolean A 16-bit Unicode character.char 9Masud Tarek
  • 10. Range of data for each type Byte: -128 to +127 Short: -32768 to +32767 Integer: -2147483648 to + 2147483647 Long : -9223372036854775808 to + 9223372036854775807 Float: 1.4e045 to 3.4e+038 Double: 4.9e-324 to 1.8e+308 (approx.) Boolean : true, false Char: 0 to 65535 How negative data are kept As 2s complement of that number with sign bit=1 Because of 2s complement there is no -0. 2s complement of 0 is also 0 (not -0) 10Masud Tarek
  • 11. Integer: 123 (no comma, decimal point) (decimal = start with 1 to 9, not zero) 0b10101 binary leading zero b (digit 0 1) 0123 octal leading with zero (digit 0 to 7) 0x23Af3 leading zero x (digit 0 to 9, a to f) 123_3454, 0x2___34F__45B (one or more underscore, must start/end with digit) For long integer, the value should end with L 987642L 11Masud Tarek
  • 12. Real numbers Float numbers have to end with F or f 656.78F Numbers may have underscores (not at the start/end) 67__65.5_6, 0x65_6.98_67_2 In scientific notation, for decimal E (base 10) is used where as for hexa-decimal P(base 16) is used 32e+8, 0x4ABp-2 12Masud Tarek
  • 13. Character Enclosed by single quote a b Z 2 @ Escape Sequence (read book) used for Non Printable characters Special characters Unicode/Hexa/Octal 141 octal equivalent to a u0061 hexa/unicode equivalent to a String Enclosed by double quote hello world Escape sequence () also can be used 13Masud Tarek
  • 14. In Java, there are 2 types of scope (visibility/lifetime) Variables that are created inside a method have scope within the curly braces { } they are created within. ... Mathod_one( ) { ... ... { int x=0; // scope of x is within the inner block {} ... } ... // this block is outside of the scope of x ... } Variables declared within class level have a scope within the class Static variable of a class can be used outside of the class only with a reference of the class 14Masud Tarek
  • 15. Automatic Type Conversion: When two types are compatible The destination type with larger memory size than the source type. Example: int type is larger than byte value The numeric types are compatible with each other. The numeric types are not compatible with character or boolean char and boolean are not compatible with each other 15Masud Tarek
  • 16. (target_type) variable_name Example: int x=50; byte y; y=(byte)x; If target type has smaller range, reduced modulo value will be assigned Suppose in the previous example, x=258 So, y=258%(Bytes max value+1)=258%128=2 From float to int/long, decimal part will be truncated 16Masud Tarek
  • 17. In an expression data are automatically promoted to higher type if one operand is in higher type byte-short-int-long-float-double Byte and short are always promoted to int This may lead to error of an apparently correct coding byte x; x=50*2; this may give compilation error because during calculation, result is automatically converted to int value, so without typecasting, result can not be assigned to x Solution: x=(byte)(50*2); So, for safe, use int/long/double data types if possible 17Masud Tarek
  • 18. Chapter 2 and 3 Review Questions: 1. Mention some rules and conventions of naming an identifier 2. How many keywords in Java? Are followings are keywords in Java: null, true, false (why-explain) 3. Define expression and statement. 4. How many primitive data types in Java? Mention their memory size. 5. Discuss about integer and real number literals. 6. What will be the scope of a variable in java? 7. What is the automatic typecasting mechanism of expression evaluation in java 18Masud Tarek