SlideShare a Scribd company logo
JAVA
Basic Programming
What is Java?
Java is a popular programming language, created in 1995.
It is owned by Oracle, and more than 3 billion devices run Java.
It is used for:
• Mobile applications (specially Android apps)
• Desktop applications
• Web applications
• Web servers and application servers
• Games
• Database connection
• And much, much more
Why Use Java?
• Java works on different platforms (Windows, Mac, Linux, Raspberry
Pi, etc.)
• It is one of the most popular programming language in the world
• It is easy to learn and simple to use
• It is open-source and free
• It is secure, fast and powerful
• It has a huge community support (tens of millions of developers)
• Java is an object oriented language which gives a clear structure to
programs and allows code to be reused, lowering development costs
• As Java is close to C++ and C#, it makes it easy for programmers to
switch to Java or vice versa.
Java Quickstart
In Java, every application begins with a class name, and that class
must match the filename.
Let's create our first Java file, called Main.java, which can be done in
any text editor (like Notepad).
The file should contain a "Hello World" message, which is written with
the following code:
JAVA VARIABLES
Variables are containers for storing data values.
In Java, there are different types of variables, for example:
• String - stores text, such as "Hello". String values are surrounded by double
quotes
• int - stores integers (whole numbers), without decimals, such as 123 or -123
• float or double - stores floating point numbers, with decimals, such as 19.99
or -19.99
• char - stores single characters, such as 'a' or 'B'. Char values are surrounded
by single quotes
• boolean - stores values with two states: true or false
Declaring (Creating) Variables
To create a variable, you must specify the type and assign it a value:
Syntax
Where type is one of Java's types (such as int or String), and variable
is the name of the variable (such as x or name). The equal sign is used
to assign values to the variable.
To create a variable that should store text, look at the following
example:
Example
Create a variable called name of type String and assign it the
value "John":
type variable = value;
String name = "John";
System.out.println(name);
Declaring (Creating) Variables
To create a variable that should store a number, look at the following
example:
Example
Create a variable called myNum of type int and assign it the
value 15:
You can also declare a variable without assigning the value, and
assign the value later:
int myNum = 15;
System.out.println(myNum);
int myNum;
myNum = 15;
System.out.println(myNum);
Note that if you assign a new value to an existing variable, it will
overwrite the previous value:
Example
Change the value of myNum from 15 to 20:
Other declarations:
int myNum = 15;
myNum = 20; // myNum is now 20
System.out.println(myNum);
int myNum = 5; // Integer (whole number)
Double myDouble = 5.99; // Floating point number
char myLetter = 'D'; // Character
boolean myBool = true; // Boolean
String myText = "Hello"; // String
DATA TYPES
Data types are divided into two groups:
• Primitive data types - includes byte, short, int, long, float, double, boolean
and char
• Non-primitive data types - such as String, Arrays and Classes (you will learn
more about these in a later chapter)
Primitive Data Types
A primitive data type specifies the size and type of variable values,
and it has no additional methods.
There are eight primitive data types in Java:
Data Type Size Description
byte 1 byte Stores whole numbers from -128 to 127
short 2 bytes Stores whole numbers from -32,768 to 32,767
int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647
long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits
boolean 1 bit Stores true or false values
char 2 bytes Stores a single character/letter or ASCII values
Numbers
Primitive number types are divided into two groups:
• Integer types stores whole numbers, positive or negative (such as
123 or -456), without decimals. Valid types are byte, short, int and
long. Which type you should use, depends on the numeric value.
• Floating point types represents numbers with a fractional part,
containing one or more decimals. There are two types: float and
double.
Integer Types
Byte
The byte data type can store whole numbers from -128 to 127. This
can be used instead of int or other integer types to save memory
when you are certain that the value will be within -128 and 127:
Short
The short data type can store whole numbers from -32768 to 32767:
byte myNum = 100;
System.out.println(myNum);
short myNum = 5000;
System.out.println(myNum);
Integer Types
Int
The int data type can store whole numbers from -2147483648 to
2147483647. In general, and in our tutorial, the int data type is the
preferred data type when we create variables with a numeric value.
Long
The long data type can store whole numbers from -
9223372036854775808 to 9223372036854775807. This is used when
int is not large enough to store the value. Note that you should end
the value with an "L":
int myNum = 100000;
System.out.println(myNum);
long myNum = 15000000000L;
System.out.println(myNum);
Floating Point Types
You should use a floating point type whenever you need a number
with a decimal, such as 9.99 or 3.14515.
Float
The float data type can store fractional numbers from 3.4e−038 to
3.4e+038. Note that you should end the value with an "f":
Double
The double data type can store fractional numbers from 1.7e−308 to
1.7e+308. Note that you should end the value with a "d":
float myNum = 5.75f;
System.out.println(myNum);
double myNum = 19.99d;
System.out.println(myNum);
Floating Point Types
Use float or double?
The precision of a floating point value indicates how many digits the value can
have after the decimal point. The precision of float is only six or seven decimal
digits, while double variables have a precision of about 15 digits. Therefore it is
safer to use double for most calculations.
Floating Point Types
You should use a floating point type whenever you need a number
with a decimal, such as 9.99 or 3.14515.
Float
The float data type can store fractional numbers from 3.4e−038 to
3.4e+038. Note that you should end the value with an "f":
Double
The double data type can store fractional numbers from 1.7e−308 to
1.7e+308. Note that you should end the value with a "d":
float myNum = 5.75f;
System.out.println(myNum);
double myNum = 19.99d;
System.out.println(myNum);
Booleans
A boolean data type is declared with the boolean keyword and can only take the
values true or false:
Characters
The char data type is used to store a single character. The character must be
surrounded by single quotes, like 'A' or 'c':
boolean isJavaFun = true;
boolean isFishTasty = false;
System.out.println(isJavaFun); // Outputs true
System.out.println(isFishTasty); // Outputs false
char myGrade = 'B';
System.out.println(myGrade);
Strings
The String data type is used to store a sequence of characters (text). String values
must be surrounded by double quotes:
String greeting = "Hello World";
System.out.println(greeting);
JAVA OPERATORS
• Operators are used to perform operations on variables and values.
In the example below, we use the + operator to add together two values:
int x = 100 + 50;
Although the + operator is often used to add together two values, like in the
example above, it can also be used to add together a variable and a value, or
a variable and another variable:
int sum1 = 100 + 50; // 150 (100 + 50)
int sum2 = sum1 + 250; // 400 (150 + 250)
int sum3 = sum2 + sum2; // 800 (400 + 400)

More Related Content

Similar to JAVA LESSON-01.pptx

C#
C#C#
Lecture 7
Lecture 7Lecture 7
Lecture 7
Mohammed Khan
 
02 Primitive data types and variables
02 Primitive data types and variables02 Primitive data types and variables
02 Primitive data types and variables
maznabili
 
Data types
Data typesData types
Data types
Sachin Satwaskar
 
Java Basics 1.pptx
Java Basics 1.pptxJava Basics 1.pptx
Java Basics 1.pptx
TouseeqHaider11
 
Variables in C++, data types in c++
Variables in C++, data types in c++Variables in C++, data types in c++
Variables in C++, data types in c++
Neeru Mittal
 
variablesfinal-170820055428 data type results
variablesfinal-170820055428 data type resultsvariablesfinal-170820055428 data type results
variablesfinal-170820055428 data type results
atifmugheesv
 
Variables&DataTypes.pptx
Variables&DataTypes.pptxVariables&DataTypes.pptx
Variables&DataTypes.pptx
sanjanaMudduluru1
 
Programming fundamental 02.pptx
Programming fundamental 02.pptxProgramming fundamental 02.pptx
Programming fundamental 02.pptx
ZubairAli256321
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
Jayfee Ramos
 
Data Handling
Data HandlingData Handling
Data Handling
Praveen M Jigajinni
 
Acm aleppo cpc training second session
Acm aleppo cpc training second sessionAcm aleppo cpc training second session
Acm aleppo cpc training second session
Ahmad Bashar Eter
 
java programming basics - part ii
 java programming basics - part ii java programming basics - part ii
java programming basics - part ii
jyoti_lakhani
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
Harshita Yadav
 
cassignmentii-170424105623.pdf
cassignmentii-170424105623.pdfcassignmentii-170424105623.pdf
cassignmentii-170424105623.pdf
YRABHI
 
5-Lec - Datatypes.ppt
5-Lec - Datatypes.ppt5-Lec - Datatypes.ppt
5-Lec - Datatypes.ppt
AqeelAbbas94
 
CIS 1403 Lab 2- Data Types and Variables
CIS 1403 Lab 2- Data Types and VariablesCIS 1403 Lab 2- Data Types and Variables
CIS 1403 Lab 2- Data Types and Variables
Hamad Odhabi
 
Lesson 4 Basic Programming Constructs.pptx
Lesson 4 Basic Programming Constructs.pptxLesson 4 Basic Programming Constructs.pptx
Lesson 4 Basic Programming Constructs.pptx
John Burca
 
C++ data types
C++ data typesC++ data types
C++ data types
pratikborsadiya
 
Datatypes in c
Datatypes in cDatatypes in c

Similar to JAVA LESSON-01.pptx (20)

C#
C#C#
C#
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
02 Primitive data types and variables
02 Primitive data types and variables02 Primitive data types and variables
02 Primitive data types and variables
 
Data types
Data typesData types
Data types
 
Java Basics 1.pptx
Java Basics 1.pptxJava Basics 1.pptx
Java Basics 1.pptx
 
Variables in C++, data types in c++
Variables in C++, data types in c++Variables in C++, data types in c++
Variables in C++, data types in c++
 
variablesfinal-170820055428 data type results
variablesfinal-170820055428 data type resultsvariablesfinal-170820055428 data type results
variablesfinal-170820055428 data type results
 
Variables&DataTypes.pptx
Variables&DataTypes.pptxVariables&DataTypes.pptx
Variables&DataTypes.pptx
 
Programming fundamental 02.pptx
Programming fundamental 02.pptxProgramming fundamental 02.pptx
Programming fundamental 02.pptx
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
 
Data Handling
Data HandlingData Handling
Data Handling
 
Acm aleppo cpc training second session
Acm aleppo cpc training second sessionAcm aleppo cpc training second session
Acm aleppo cpc training second session
 
java programming basics - part ii
 java programming basics - part ii java programming basics - part ii
java programming basics - part ii
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
 
cassignmentii-170424105623.pdf
cassignmentii-170424105623.pdfcassignmentii-170424105623.pdf
cassignmentii-170424105623.pdf
 
5-Lec - Datatypes.ppt
5-Lec - Datatypes.ppt5-Lec - Datatypes.ppt
5-Lec - Datatypes.ppt
 
CIS 1403 Lab 2- Data Types and Variables
CIS 1403 Lab 2- Data Types and VariablesCIS 1403 Lab 2- Data Types and Variables
CIS 1403 Lab 2- Data Types and Variables
 
Lesson 4 Basic Programming Constructs.pptx
Lesson 4 Basic Programming Constructs.pptxLesson 4 Basic Programming Constructs.pptx
Lesson 4 Basic Programming Constructs.pptx
 
C++ data types
C++ data typesC++ data types
C++ data types
 
Datatypes in c
Datatypes in cDatatypes in c
Datatypes in c
 

Recently uploaded

The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 

Recently uploaded (20)

The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 

JAVA LESSON-01.pptx

  • 2. What is Java? Java is a popular programming language, created in 1995. It is owned by Oracle, and more than 3 billion devices run Java. It is used for: • Mobile applications (specially Android apps) • Desktop applications • Web applications • Web servers and application servers • Games • Database connection • And much, much more
  • 3. Why Use Java? • Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) • It is one of the most popular programming language in the world • It is easy to learn and simple to use • It is open-source and free • It is secure, fast and powerful • It has a huge community support (tens of millions of developers) • Java is an object oriented language which gives a clear structure to programs and allows code to be reused, lowering development costs • As Java is close to C++ and C#, it makes it easy for programmers to switch to Java or vice versa.
  • 4. Java Quickstart In Java, every application begins with a class name, and that class must match the filename. Let's create our first Java file, called Main.java, which can be done in any text editor (like Notepad). The file should contain a "Hello World" message, which is written with the following code:
  • 5. JAVA VARIABLES Variables are containers for storing data values. In Java, there are different types of variables, for example: • String - stores text, such as "Hello". String values are surrounded by double quotes • int - stores integers (whole numbers), without decimals, such as 123 or -123 • float or double - stores floating point numbers, with decimals, such as 19.99 or -19.99 • char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes • boolean - stores values with two states: true or false
  • 6. Declaring (Creating) Variables To create a variable, you must specify the type and assign it a value: Syntax Where type is one of Java's types (such as int or String), and variable is the name of the variable (such as x or name). The equal sign is used to assign values to the variable. To create a variable that should store text, look at the following example: Example Create a variable called name of type String and assign it the value "John": type variable = value; String name = "John"; System.out.println(name);
  • 7. Declaring (Creating) Variables To create a variable that should store a number, look at the following example: Example Create a variable called myNum of type int and assign it the value 15: You can also declare a variable without assigning the value, and assign the value later: int myNum = 15; System.out.println(myNum); int myNum; myNum = 15; System.out.println(myNum);
  • 8. Note that if you assign a new value to an existing variable, it will overwrite the previous value: Example Change the value of myNum from 15 to 20: Other declarations: int myNum = 15; myNum = 20; // myNum is now 20 System.out.println(myNum); int myNum = 5; // Integer (whole number) Double myDouble = 5.99; // Floating point number char myLetter = 'D'; // Character boolean myBool = true; // Boolean String myText = "Hello"; // String
  • 9. DATA TYPES Data types are divided into two groups: • Primitive data types - includes byte, short, int, long, float, double, boolean and char • Non-primitive data types - such as String, Arrays and Classes (you will learn more about these in a later chapter)
  • 10. Primitive Data Types A primitive data type specifies the size and type of variable values, and it has no additional methods. There are eight primitive data types in Java: Data Type Size Description byte 1 byte Stores whole numbers from -128 to 127 short 2 bytes Stores whole numbers from -32,768 to 32,767 int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647 long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits boolean 1 bit Stores true or false values char 2 bytes Stores a single character/letter or ASCII values
  • 11. Numbers Primitive number types are divided into two groups: • Integer types stores whole numbers, positive or negative (such as 123 or -456), without decimals. Valid types are byte, short, int and long. Which type you should use, depends on the numeric value. • Floating point types represents numbers with a fractional part, containing one or more decimals. There are two types: float and double.
  • 12. Integer Types Byte The byte data type can store whole numbers from -128 to 127. This can be used instead of int or other integer types to save memory when you are certain that the value will be within -128 and 127: Short The short data type can store whole numbers from -32768 to 32767: byte myNum = 100; System.out.println(myNum); short myNum = 5000; System.out.println(myNum);
  • 13. Integer Types Int The int data type can store whole numbers from -2147483648 to 2147483647. In general, and in our tutorial, the int data type is the preferred data type when we create variables with a numeric value. Long The long data type can store whole numbers from - 9223372036854775808 to 9223372036854775807. This is used when int is not large enough to store the value. Note that you should end the value with an "L": int myNum = 100000; System.out.println(myNum); long myNum = 15000000000L; System.out.println(myNum);
  • 14. Floating Point Types You should use a floating point type whenever you need a number with a decimal, such as 9.99 or 3.14515. Float The float data type can store fractional numbers from 3.4e−038 to 3.4e+038. Note that you should end the value with an "f": Double The double data type can store fractional numbers from 1.7e−308 to 1.7e+308. Note that you should end the value with a "d": float myNum = 5.75f; System.out.println(myNum); double myNum = 19.99d; System.out.println(myNum);
  • 15. Floating Point Types Use float or double? The precision of a floating point value indicates how many digits the value can have after the decimal point. The precision of float is only six or seven decimal digits, while double variables have a precision of about 15 digits. Therefore it is safer to use double for most calculations.
  • 16. Floating Point Types You should use a floating point type whenever you need a number with a decimal, such as 9.99 or 3.14515. Float The float data type can store fractional numbers from 3.4e−038 to 3.4e+038. Note that you should end the value with an "f": Double The double data type can store fractional numbers from 1.7e−308 to 1.7e+308. Note that you should end the value with a "d": float myNum = 5.75f; System.out.println(myNum); double myNum = 19.99d; System.out.println(myNum);
  • 17. Booleans A boolean data type is declared with the boolean keyword and can only take the values true or false: Characters The char data type is used to store a single character. The character must be surrounded by single quotes, like 'A' or 'c': boolean isJavaFun = true; boolean isFishTasty = false; System.out.println(isJavaFun); // Outputs true System.out.println(isFishTasty); // Outputs false char myGrade = 'B'; System.out.println(myGrade);
  • 18. Strings The String data type is used to store a sequence of characters (text). String values must be surrounded by double quotes: String greeting = "Hello World"; System.out.println(greeting);
  • 19. JAVA OPERATORS • Operators are used to perform operations on variables and values. In the example below, we use the + operator to add together two values: int x = 100 + 50; Although the + operator is often used to add together two values, like in the example above, it can also be used to add together a variable and a value, or a variable and another variable: int sum1 = 100 + 50; // 150 (100 + 50) int sum2 = sum1 + 250; // 400 (150 + 250) int sum3 = sum2 + sum2; // 800 (400 + 400)