SlideShare uma empresa Scribd logo
1 de 32
Java Fundamentals (Java SE 8)
Data, Variables, and Operators
Presented by :
Eng Marwa Ali Eissa
;)
2
LOGOkeywords
Keywords are words that :
 Are reserved for use by Java.
 May not be used to name Java applications or objects ,such as
classes ,methods or variables.
 Are case-sensitive and in lowercase
LOGOIdentifiers
An identifier is a name that:
 Identifies a variable, class or method
 Identifiers must start with either an uppercase or lowercase letter,
an underscore (_), or a dollar sign ($).
 Identifiers cannot contain punctuation, spaces, dashes,
or any of the Java technology keywords.
 Most importantly identifiers are case sensitive.
 Identifiers cannot begin with a digit (0-9)
 White space is not permitted.
 Examples of legal identifiers: age, $salary, _value, __1_value
 Examples of illegal identifiers : 123abc, -salary , max value
LOGONaming Convention in Java
 packages names are in lowercase .
E.g :java.lang,java.util and com.ourglobalcompany.myproject.model
 Classes: Names should be in CamelCase is where each new word
begins with a capital letter
E.g. :CustomerAccount , Employee
 Methods & Variables : is the same as CamelCase except the first letter
of the name is in lowercase
E.g.: firstName ,orderNumber
void calculateTax() , String getSalary()
 Constants: Names should be in uppercase.
E.g.: DEFAULT_WIDTH , MAX_HEIGHT
LOGOStatements and Blocks
Statements are roughly equivalent to sentences in natural languages.
A statement forms a complete unit of execution. The following types of
expressions can be made into a statement by terminating the
expression with a semicolon (;).
 Assignment expressions
 Any use of ++ or --
 Method invocations
 Object creation expressions
LOGOStatements and Blocks
Multiple Java statements may be grouped using braces to form a block
statement. Block statements are logical units of statements ,usually
associated with a specific statement container such as a method or a
loop .Java allows to place a block statement within another block
LOGOVariables
Variables are named memory locations that are used for storing data
items of a value of a particular data type or a reference to an object.
 Variable lives confined to the scope of their definition, which cab be :
 At the local evlevel inside a method or block of code , they are called
local variables
 At the object instance lel when defined as a Non-static attribute
, also called instance variables
 At the class level when defined as a static attribute , also called
class variables
Variables in method declarations—these are called parameters.
LOGOVariables
Variable Naming Conventions
The variable naming conversions are the same as those of identifiers
Java developers must not use the dollar symbol in a variable. Also, variable
names must not start with an underscore
Initializing Variables
You can assign values to variables in your program during variable declaration.
datatype variableName = initvalue;
datatype variable1 = initvalue , variable2= initvalue2 , .... , variableN= initvalueN;
Uninitialized Variables
Attributes (instance or static) are always initialized to a default value
local variable must be initialized otherwise the Program does not compile
Variable Declaration
 To declare a variable, you use the syntax :
datatype varableName;
 To Declare multiple variables using A single statement :
datatype variable1,variable2 ,variable3 ,..... variableN;
LOGO
9
Data Types in JAVA
 Value Types (Built in data types – Primitive data types Like: int ,
float , ..)
 Reference Types (any other type Like: objects , Interface ,
array, Enum ..)
5
x
int x =5;
stack heap
Memory
ref
Data
LOGOPrimitive Data Types
s There are eight primitive data types supported by Java,
and they enable you to define variables for storing data that
fall into one of three categories:
1. Numeric values
 integer (byte, short, int, and long)
 floating-point (float and double)
2. A single Unicode character (char)
3. Logical values (boolean) that can be true or false
LOGOInteger Data Types
Data Type Size Range Default
Value
byte 1 byte (8 bits) -27 to 27-1 (-128 to +127 or 256 possible
values)
0
short 2 bytes (16 bits) -215 to 215-1 (-32,768 to 32,767 or
65,535 possible values )
0
int 4 bytes (32 bits) -231 to 231-1 (-2,147,483,648 to
2,147,483,647 or 4,294,967,296 possible
Values )
0
long 8 bytes (64 bits) -263 to 263-1 (-
9,223,372,036854,775,808 to
9,223,372,036854,775,807, or
18,446,744,073,709,551,616
possible values)
0L
LOGOFloating-Point Data Types
Data Type Size Default Value
float 32-bit 0.0f
double 64-bit 0.0d
 Float is mainly used to save memory in large arrays of floating
point numbers.
 Float data type is never used for precise values such as
currency.
 Double data type is generally used as the default data type for
decimal values. generally the default choice.
 Double data type should never be used for precise values such
as currency.
LOGOCharacter Type
 Char data type is used to store any character.
 Java uses Unicode to represent characters
 char data type is a single 16-bit Unicode character.
 Minimum value is 'u0000' (or 0).
 Maximum value is 'uffff' (or 65,535 inclusive).
 Default value : 'u0000'
 Example : char letterA ='A'
LOGOBoolean Type
 boolean data type represents one bit of information.
 There are only two possible values : true and false.
 This data type is used for simple flags that track true/false
conditions.
 Used to hold the result of an expression that evaluates
to either true or false.
 Default value : false
 The size of boolean is not defined in the Java specification, but
requires at least one bit.
 Example : boolean one = true
LOGOPrimitive Casting
byte  short int long float double
Implicit Casting
Explicit Casting
(Automatic Type Conversions)
Explicit casting
Implicit casting
Data loss due to
explicit down casting
LOGOLiterals
A literal in java is any item that directly represents a particular value .
Variables belonging to any primitive types can also be assigned values
and treated as literals .
Each variable type has a corresponding literal type , such as integer,
character, string and boolean
Literal
LOGOTypes of Literals
There are four types of literals in java, which are based on the
types of variables present
 Numeric Literal
 int  ex: 7
 double  ex: 12.87 , 12e22 , 19E-95
 float  ex: 12.87f , 123.988F
 Boolean Literal
 true or false
 Character Literal
 ‘a’ ,’A’,’#’,’3’  ASCII
 String Literal
 “hi from Marwa” , “Welcome n in New Horizons”
LOGOEscape Sequences for Character Literals
Escape Sequence Description
t Insert a tab in the text at this point.
b Insert a backspace in the text at this point.
n Insert a newline in the text at this point.
r Insert a carriage return in the text at this point.
f Insert a formfeed in the text at this point.
'
Insert a single quote character in the text at
this point.
"
Insert a double quote character in the text at
this point.

Insert a backslash character in the text at this
point.
d Octal
xd Hexadecimal
ud Unicode Character
LOGODeclaring Constants
 A Constant is a variable type in java whose value does not
change .
 Constants are defined using the final keyword followed by the
variable declaration. By convention ,constant variable names
are in uppercase .If the name is composed of more than one
word, the words are separated by an underscore (_).
 Constants defined in this way cannot be reassigned, and it is
a compile - time error if your program tries to do so.
EX: final double PI=3.141592653589793;
EX: final int FEET_PER_YARD = 3;
LOGOExpressions
An expression is a combination of operators (such as
addition '+', subtraction '-', multiplication '*', division '/')
and operands (variables or literals), that can be evaluated
to yield a single value of a certain type
1 + 2 * 3 // evaluated to int 7
int sum, number; sum + number // evaluated to an int value
// Evaluated to a double value
double principal, interestRate; principal * (1 + interestRate)
LOGOOperators
 Arithmetic Operators
 Assignment Operators
 Relational Operators
 Logical Operators
 Increment and Decrement Operators
 Bitwise Operators
 Operator Precedence
LOGOArithmetic Operators
Operator Meaning Syntax
+ Addition X + y
‫ــ‬ Subtraction X - y
* Multiplication X * y
/ Division X / y
% Modulus
The reminder from a division
operation
X % y
Arithmetic operators are used in mathematical expressions
String Concatenation
The + operator may used to concatenate the strings together
System.out.println("Account Balance is"+ balance);
LOGOCompound Arithmetic Assignment Operators
Operator Expression Meaning
+= X += y X=X + y
‫=ــ‬ X -= y X=X – y
*= X *= y X=X * y
/= X /= y X=X / y
%= X %= y X=X % y
= Simple assignment operator, Assigns values from right side
operands to left side operand
LOGORelational Operators
Relational Operators are symbols user for determining
relational comparisons between operands. all expressions
created using relational operators will return a boolean value
, depending on whether the comparison is true
Operator Meaning Syntax
== equal to X == y
!= not equal to X != y
> greater than X > y
< less than X < y
>= greater than or equal to X >= y
<= less than or equal to X <= y
LOGOLogical Operators
Operator Meaning
& logical AND
&& conditional AND
| logical OR
|| Conditional OR
^ exclusive OR (XOR)
! logical (NOT)
Logical Operators evaluate expressions that carry boolean
values . The result returned by logical operators is also a
boolean value.
LOGOLogical Operators - The truth tables
LOGOIncrement and Decrement Operators
Operator Meaning
++ Increment operator; increase the
value of a numeric variable or array
element by 1
-- Decrement operator; reduce the
value of a numeric variable or array
element by 1
Prefix and Postfix
 Prefix :
Placing the operator before the operand causes increment or decrement to
occur before the value of the operand is used to evaluate an expression
For example : int a=5;
int x =++a;
both x and a will have the value 6
 Postfix :
Placing the operator after the operand causes increment or decrement to
occur after the value of the operand is used in an expression
For example : int a=5;
int x =a++;
x will have the value of 5 and a will have the value 6
LOGOBitwise Operators
Operator Description
Binary bitwise AND (&) Returns a 1 if both bits are 1
Binary bitwise OR (|) Returns a 1 if either bit is 1
Binary bitwise XOR (^) Returns a 1 if both bits have different values
Unary bitwise complement (~) Changes each bit in its operand to the
opposite value
Binary left shift (<<) Shifts bits on the left to the left by a
distance denoted by the right operand .Fills
in zeros
Binary right shift (>>) Shifts bits on the left to the right by a
distance denoted by the right operand .Fills
in the highest bit on the left side.
Binary arithmetic and logical shift
(>>>)
Shifts bits on the left to the right by a
distance denoted by the right operand .Fills
in zeros
LOGO
29
00001010
&
00011001
________
00001000
00001010
|
00011001
________
00011011
00001010
~
________
11110101
X=10
y=25
z=8 z=27 z=-11
00001010
>>
2
________
00000010
z=2
11110101
>>
2
________
11111101
z=-3
X=10 X=-11
00001010
<<
2
________
00101000
z=40
0X ff ff ff ff ff
>>>
42
________
0X 00 00 00 ff
z=255
X=-1
00001010
>>>
2
________
00000010
z=2
X=10
Bitwise Operators
LOGOOperator Precedence
Operator precedence is the order in which operators are
evaluated in an expression containing two or more
operators
Operators with same
precedence are calculated
from left to right
Parentheses alter the
order of calculations
LOGOOperator Precedence
precedence Operator
1 (..) [..] . (dot operator)
2 ++ -- ! ~ instanceof
3 new (type) expression
4 * / %
5 + -
6 << >> >>>
7 < > <= >=
8 = !=
9 &
10 ^
11 |
12 &&
13 ||
Data Types, Variables, and Operators

Mais conteúdo relacionado

Mais procurados (20)

Java - Generic programming
Java - Generic programmingJava - Generic programming
Java - Generic programming
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
 
Java String
Java String Java String
Java String
 
Java Streams
Java StreamsJava Streams
Java Streams
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Exception handling
Exception handlingException handling
Exception handling
 
input/ output in java
input/ output  in javainput/ output  in java
input/ output in java
 
Java program structure
Java program structureJava program structure
Java program structure
 
Method Overloading In Java
Method Overloading In JavaMethod Overloading In Java
Method Overloading In Java
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
String, string builder, string buffer
String, string builder, string bufferString, string builder, string buffer
String, string builder, string buffer
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Genesis and Overview of Java
Genesis and Overview of Java Genesis and Overview of Java
Genesis and Overview of Java
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Wrapper classes
Wrapper classes Wrapper classes
Wrapper classes
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Files in java
Files in javaFiles in java
Files in java
 

Destaque

Operator & Expression in c++
Operator & Expression in c++Operator & Expression in c++
Operator & Expression in c++bajiajugal
 
Agriculture research and extension in PAKISTAN
Agriculture research and extension in PAKISTANAgriculture research and extension in PAKISTAN
Agriculture research and extension in PAKISTANzeeshan turi
 
Expressions in c++
 Expressions in c++ Expressions in c++
Expressions in c++zeeshan turi
 

Destaque (6)

Operator & Expression in c++
Operator & Expression in c++Operator & Expression in c++
Operator & Expression in c++
 
15 bitwise operators
15 bitwise operators15 bitwise operators
15 bitwise operators
 
Operators in C++
Operators in C++Operators in C++
Operators in C++
 
Agriculture research and extension in PAKISTAN
Agriculture research and extension in PAKISTANAgriculture research and extension in PAKISTAN
Agriculture research and extension in PAKISTAN
 
Expressions in c++
 Expressions in c++ Expressions in c++
Expressions in c++
 
Operators in java
Operators in javaOperators in java
Operators in java
 

Semelhante a Data Types, Variables, and Operators

Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...GANESHBABUVelu
 
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 KuteTushar B Kute
 
Java: Primitive Data Types
Java: Primitive Data TypesJava: Primitive Data Types
Java: Primitive Data TypesTareq Hasan
 
Introduction to Java Object Oiented Concepts and Basic terminologies
Introduction to Java Object Oiented Concepts and Basic terminologiesIntroduction to Java Object Oiented Concepts and Basic terminologies
Introduction to Java Object Oiented Concepts and Basic terminologiesTabassumMaktum
 
Fundamental programming structures in java
Fundamental programming structures in javaFundamental programming structures in java
Fundamental programming structures in javaShashwat Shriparv
 
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 conceptmanish kumar
 
Java basics and java variables
Java basics and java variablesJava basics and java variables
Java basics and java variablesPushpendra Tyagi
 
OCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & StatementsOCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & Statementsİbrahim Kürce
 
Literals, primitive datatypes, variables, expressions, identifiers
Literals, primitive datatypes, variables, expressions, identifiersLiterals, primitive datatypes, variables, expressions, identifiers
Literals, primitive datatypes, variables, expressions, identifiersTanishq Soni
 
Input processing and output in Python
Input processing and output in PythonInput processing and output in Python
Input processing and output in PythonMSB Academy
 
Input processing and output in Python
Input processing and output in PythonInput processing and output in Python
Input processing and output in PythonRaajendra M
 

Semelhante a Data Types, Variables, and Operators (20)

Java Programming
Java Programming Java Programming
Java Programming
 
Basic
BasicBasic
Basic
 
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
 
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
 
Java: Primitive Data Types
Java: Primitive Data TypesJava: Primitive Data Types
Java: Primitive Data Types
 
Introduction to Java Object Oiented Concepts and Basic terminologies
Introduction to Java Object Oiented Concepts and Basic terminologiesIntroduction to Java Object Oiented Concepts and Basic terminologies
Introduction to Java Object Oiented Concepts and Basic terminologies
 
Java unit 2
Java unit 2Java unit 2
Java unit 2
 
Introduction to-programming
Introduction to-programmingIntroduction to-programming
Introduction to-programming
 
Fundamental programming structures in java
Fundamental programming structures in javaFundamental programming structures in java
Fundamental programming structures in java
 
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
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
 
chap04.ppt
chap04.pptchap04.ppt
chap04.ppt
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Java basics and java variables
Java basics and java variablesJava basics and java variables
Java basics and java variables
 
OOP Poster Presentation
OOP Poster PresentationOOP Poster Presentation
OOP Poster Presentation
 
OCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & StatementsOCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & Statements
 
vb.net.pdf
vb.net.pdfvb.net.pdf
vb.net.pdf
 
Literals, primitive datatypes, variables, expressions, identifiers
Literals, primitive datatypes, variables, expressions, identifiersLiterals, primitive datatypes, variables, expressions, identifiers
Literals, primitive datatypes, variables, expressions, identifiers
 
Input processing and output in Python
Input processing and output in PythonInput processing and output in Python
Input processing and output in Python
 
Input processing and output in Python
Input processing and output in PythonInput processing and output in Python
Input processing and output in Python
 

Último

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024VictoriaMetrics
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2
 

Último (20)

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 

Data Types, Variables, and Operators

  • 1. Java Fundamentals (Java SE 8) Data, Variables, and Operators Presented by : Eng Marwa Ali Eissa ;) 2
  • 2. LOGOkeywords Keywords are words that :  Are reserved for use by Java.  May not be used to name Java applications or objects ,such as classes ,methods or variables.  Are case-sensitive and in lowercase
  • 3. LOGOIdentifiers An identifier is a name that:  Identifies a variable, class or method  Identifiers must start with either an uppercase or lowercase letter, an underscore (_), or a dollar sign ($).  Identifiers cannot contain punctuation, spaces, dashes, or any of the Java technology keywords.  Most importantly identifiers are case sensitive.  Identifiers cannot begin with a digit (0-9)  White space is not permitted.  Examples of legal identifiers: age, $salary, _value, __1_value  Examples of illegal identifiers : 123abc, -salary , max value
  • 4. LOGONaming Convention in Java  packages names are in lowercase . E.g :java.lang,java.util and com.ourglobalcompany.myproject.model  Classes: Names should be in CamelCase is where each new word begins with a capital letter E.g. :CustomerAccount , Employee  Methods & Variables : is the same as CamelCase except the first letter of the name is in lowercase E.g.: firstName ,orderNumber void calculateTax() , String getSalary()  Constants: Names should be in uppercase. E.g.: DEFAULT_WIDTH , MAX_HEIGHT
  • 5. LOGOStatements and Blocks Statements are roughly equivalent to sentences in natural languages. A statement forms a complete unit of execution. The following types of expressions can be made into a statement by terminating the expression with a semicolon (;).  Assignment expressions  Any use of ++ or --  Method invocations  Object creation expressions
  • 6. LOGOStatements and Blocks Multiple Java statements may be grouped using braces to form a block statement. Block statements are logical units of statements ,usually associated with a specific statement container such as a method or a loop .Java allows to place a block statement within another block
  • 7. LOGOVariables Variables are named memory locations that are used for storing data items of a value of a particular data type or a reference to an object.  Variable lives confined to the scope of their definition, which cab be :  At the local evlevel inside a method or block of code , they are called local variables  At the object instance lel when defined as a Non-static attribute , also called instance variables  At the class level when defined as a static attribute , also called class variables Variables in method declarations—these are called parameters.
  • 8. LOGOVariables Variable Naming Conventions The variable naming conversions are the same as those of identifiers Java developers must not use the dollar symbol in a variable. Also, variable names must not start with an underscore Initializing Variables You can assign values to variables in your program during variable declaration. datatype variableName = initvalue; datatype variable1 = initvalue , variable2= initvalue2 , .... , variableN= initvalueN; Uninitialized Variables Attributes (instance or static) are always initialized to a default value local variable must be initialized otherwise the Program does not compile Variable Declaration  To declare a variable, you use the syntax : datatype varableName;  To Declare multiple variables using A single statement : datatype variable1,variable2 ,variable3 ,..... variableN;
  • 9. LOGO 9 Data Types in JAVA  Value Types (Built in data types – Primitive data types Like: int , float , ..)  Reference Types (any other type Like: objects , Interface , array, Enum ..) 5 x int x =5; stack heap Memory ref Data
  • 10. LOGOPrimitive Data Types s There are eight primitive data types supported by Java, and they enable you to define variables for storing data that fall into one of three categories: 1. Numeric values  integer (byte, short, int, and long)  floating-point (float and double) 2. A single Unicode character (char) 3. Logical values (boolean) that can be true or false
  • 11. LOGOInteger Data Types Data Type Size Range Default Value byte 1 byte (8 bits) -27 to 27-1 (-128 to +127 or 256 possible values) 0 short 2 bytes (16 bits) -215 to 215-1 (-32,768 to 32,767 or 65,535 possible values ) 0 int 4 bytes (32 bits) -231 to 231-1 (-2,147,483,648 to 2,147,483,647 or 4,294,967,296 possible Values ) 0 long 8 bytes (64 bits) -263 to 263-1 (- 9,223,372,036854,775,808 to 9,223,372,036854,775,807, or 18,446,744,073,709,551,616 possible values) 0L
  • 12. LOGOFloating-Point Data Types Data Type Size Default Value float 32-bit 0.0f double 64-bit 0.0d  Float is mainly used to save memory in large arrays of floating point numbers.  Float data type is never used for precise values such as currency.  Double data type is generally used as the default data type for decimal values. generally the default choice.  Double data type should never be used for precise values such as currency.
  • 13. LOGOCharacter Type  Char data type is used to store any character.  Java uses Unicode to represent characters  char data type is a single 16-bit Unicode character.  Minimum value is 'u0000' (or 0).  Maximum value is 'uffff' (or 65,535 inclusive).  Default value : 'u0000'  Example : char letterA ='A'
  • 14. LOGOBoolean Type  boolean data type represents one bit of information.  There are only two possible values : true and false.  This data type is used for simple flags that track true/false conditions.  Used to hold the result of an expression that evaluates to either true or false.  Default value : false  The size of boolean is not defined in the Java specification, but requires at least one bit.  Example : boolean one = true
  • 15. LOGOPrimitive Casting byte  short int long float double Implicit Casting Explicit Casting (Automatic Type Conversions) Explicit casting Implicit casting Data loss due to explicit down casting
  • 16. LOGOLiterals A literal in java is any item that directly represents a particular value . Variables belonging to any primitive types can also be assigned values and treated as literals . Each variable type has a corresponding literal type , such as integer, character, string and boolean Literal
  • 17. LOGOTypes of Literals There are four types of literals in java, which are based on the types of variables present  Numeric Literal  int  ex: 7  double  ex: 12.87 , 12e22 , 19E-95  float  ex: 12.87f , 123.988F  Boolean Literal  true or false  Character Literal  ‘a’ ,’A’,’#’,’3’  ASCII  String Literal  “hi from Marwa” , “Welcome n in New Horizons”
  • 18. LOGOEscape Sequences for Character Literals Escape Sequence Description t Insert a tab in the text at this point. b Insert a backspace in the text at this point. n Insert a newline in the text at this point. r Insert a carriage return in the text at this point. f Insert a formfeed in the text at this point. ' Insert a single quote character in the text at this point. " Insert a double quote character in the text at this point. Insert a backslash character in the text at this point. d Octal xd Hexadecimal ud Unicode Character
  • 19. LOGODeclaring Constants  A Constant is a variable type in java whose value does not change .  Constants are defined using the final keyword followed by the variable declaration. By convention ,constant variable names are in uppercase .If the name is composed of more than one word, the words are separated by an underscore (_).  Constants defined in this way cannot be reassigned, and it is a compile - time error if your program tries to do so. EX: final double PI=3.141592653589793; EX: final int FEET_PER_YARD = 3;
  • 20. LOGOExpressions An expression is a combination of operators (such as addition '+', subtraction '-', multiplication '*', division '/') and operands (variables or literals), that can be evaluated to yield a single value of a certain type 1 + 2 * 3 // evaluated to int 7 int sum, number; sum + number // evaluated to an int value // Evaluated to a double value double principal, interestRate; principal * (1 + interestRate)
  • 21. LOGOOperators  Arithmetic Operators  Assignment Operators  Relational Operators  Logical Operators  Increment and Decrement Operators  Bitwise Operators  Operator Precedence
  • 22. LOGOArithmetic Operators Operator Meaning Syntax + Addition X + y ‫ــ‬ Subtraction X - y * Multiplication X * y / Division X / y % Modulus The reminder from a division operation X % y Arithmetic operators are used in mathematical expressions String Concatenation The + operator may used to concatenate the strings together System.out.println("Account Balance is"+ balance);
  • 23. LOGOCompound Arithmetic Assignment Operators Operator Expression Meaning += X += y X=X + y ‫=ــ‬ X -= y X=X – y *= X *= y X=X * y /= X /= y X=X / y %= X %= y X=X % y = Simple assignment operator, Assigns values from right side operands to left side operand
  • 24. LOGORelational Operators Relational Operators are symbols user for determining relational comparisons between operands. all expressions created using relational operators will return a boolean value , depending on whether the comparison is true Operator Meaning Syntax == equal to X == y != not equal to X != y > greater than X > y < less than X < y >= greater than or equal to X >= y <= less than or equal to X <= y
  • 25. LOGOLogical Operators Operator Meaning & logical AND && conditional AND | logical OR || Conditional OR ^ exclusive OR (XOR) ! logical (NOT) Logical Operators evaluate expressions that carry boolean values . The result returned by logical operators is also a boolean value.
  • 26. LOGOLogical Operators - The truth tables
  • 27. LOGOIncrement and Decrement Operators Operator Meaning ++ Increment operator; increase the value of a numeric variable or array element by 1 -- Decrement operator; reduce the value of a numeric variable or array element by 1 Prefix and Postfix  Prefix : Placing the operator before the operand causes increment or decrement to occur before the value of the operand is used to evaluate an expression For example : int a=5; int x =++a; both x and a will have the value 6  Postfix : Placing the operator after the operand causes increment or decrement to occur after the value of the operand is used in an expression For example : int a=5; int x =a++; x will have the value of 5 and a will have the value 6
  • 28. LOGOBitwise Operators Operator Description Binary bitwise AND (&) Returns a 1 if both bits are 1 Binary bitwise OR (|) Returns a 1 if either bit is 1 Binary bitwise XOR (^) Returns a 1 if both bits have different values Unary bitwise complement (~) Changes each bit in its operand to the opposite value Binary left shift (<<) Shifts bits on the left to the left by a distance denoted by the right operand .Fills in zeros Binary right shift (>>) Shifts bits on the left to the right by a distance denoted by the right operand .Fills in the highest bit on the left side. Binary arithmetic and logical shift (>>>) Shifts bits on the left to the right by a distance denoted by the right operand .Fills in zeros
  • 29. LOGO 29 00001010 & 00011001 ________ 00001000 00001010 | 00011001 ________ 00011011 00001010 ~ ________ 11110101 X=10 y=25 z=8 z=27 z=-11 00001010 >> 2 ________ 00000010 z=2 11110101 >> 2 ________ 11111101 z=-3 X=10 X=-11 00001010 << 2 ________ 00101000 z=40 0X ff ff ff ff ff >>> 42 ________ 0X 00 00 00 ff z=255 X=-1 00001010 >>> 2 ________ 00000010 z=2 X=10 Bitwise Operators
  • 30. LOGOOperator Precedence Operator precedence is the order in which operators are evaluated in an expression containing two or more operators Operators with same precedence are calculated from left to right Parentheses alter the order of calculations
  • 31. LOGOOperator Precedence precedence Operator 1 (..) [..] . (dot operator) 2 ++ -- ! ~ instanceof 3 new (type) expression 4 * / % 5 + - 6 << >> >>> 7 < > <= >= 8 = != 9 & 10 ^ 11 | 12 && 13 ||

Notas do Editor

  1. Although they are not keywords, you should not use the boolean values true and false or the value null as names in your programs. Note that const and goto have not been used in the Java language up to now, but they are still reserved words and you must not use them as names.
  2. Take note that: Java is a "strongly type" language. A variable is declared with a type. Once the type of a variable is declared, it can only store a value belonging to this particular type. For example, an int variable can hold only integer such as 123, and NOT floating-point number such as -2.17 or text string such as "Hello". Each variable can only be declared once. You can declare a variable anywhere inside the program, as long as it is declared before used. The type of a variable cannot be changed inside the program. The act of declaring a variable allocates a storage (of size capable of holding a value of the type).
  3. The data type of the variable also determines the range of the values that the memory location can hold. Therefore, the amount of memory allocated for a variable depends on its data type. A variable of the primitive data type holds a value whereas a variable of the reference data type holds the reference to an object in memory.
  4. Choice of Data Types for Variables As a programmer, you need to choose variables and decide on the type of the variables to be used in your programs. Most of the times, the decision is intuitive. For example, use an integer type for counting and whole number; a floating-point type for number with fractional part, String for text message, char for a single character, and boolean for binary outcomes. Rules of Thumb Use int for integer and double for floating point numbers. Use byte, short, long and float only if you have a good reason to choose that specific precision. Use int for counting and indexing, NOT floating-point type (float or double). This is because integer type are precise and more efficient in operations. Use an integer type if possible. Use a floating-point type only if the number contains a fractional part.
  5. Java has a class named Integer (note the upper case I in Integer), which defines two constants to represent maximum and minimum values for the int data type, Integer.MAX_VALUE and Integer.MIN_VALUE. For example, int max = Integer.MAX_VALUE; // Assigns maximum int value to max int min = Integer.MIN_VALUE; // Assigns minimum int value to min
  6. String Another commonly-used type is String, which represents texts (a sequence of characters) such as "Hello, world". String is not a primitive type, and will be further elaborated later. In Java, a char is enclosed by single quotes (e.g., 'A', '0'), while a String is enclosed by double quotes (e.g., "Hello"). For example, String message = "Hello, world!"; // strings are enclosed in double-quotes char gender = 'm'; // char is enclosed in single-quotes
  7. boolean can not be cast Casting between primitive & reference data type is not allowed
  8. The bitwise complement is equal to the two's complement of the value minus one. If two's complement arithmetic is used, then NOT x = −x − 1.