Computer Science Faculty: Nangarhar University
Computer Science Faculty: Nangarhar University
Date: 02/11/2019
Prepared by: Amin Gul Zakhil
Contents
• Keywords
• Primitive Datatypes
• Declarations
• Variable Names
• Numeric Literals
• Character Literals
• String
• String Literals
Keywords
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
Identifier
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()
Assignment expressions
Any use of ++ or --
Method invocations
Object creation expressions
Statements 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
Variables
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.
At the local evlevel inside a method or block of code , they are called
local variables
At the object instance level when defined as a Non-static attribute , also called instance
variables
Initializing Variables
You can assign values to variables in your program during variable declaration.
Uninitialized Variables
Attributes (instance or static) are always initialized to a default value local variable must be
initialized otherwise the Program does not compile
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 ..)
stack heap x
5
Data
int x =5;
ref
Memory
Primitive Data Types
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)
byte 1 byte (8 bits) -27 to 27-1 (-128 to +127 or 256 possible values) 0
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.
Character Type
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.
The size of boolean is not defined in the Java specification, but requires at least one bit.
Literal
Literal Types
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”
Escape Sequence for character literal
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
Declaring constants
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 (_).
1 + 2 * 3 / / evaluated t o i n t 7
i n t sum, number; sum + number / / evaluated t o an i n t value
/ / Evaluated t o a double value
double p r i n c i p a l , i n t e r e s t R a t e ; p r i n c i p a l * ( 1 + i n t e r e s t R a t e )
Operators
Arithmetic Operators
Assignment Operators
Relational Operators
Logical Operators
Bitwise Operators
Operator Precedence
Arithmetic Operator
Arithmetic operators are used in mathematical expressions
String Concatenation
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
& logical AND
&& conditional AND
| logical OR
|| Conditional OR
^ exclusive OR (XOR)
! logical (NOT)
Logical Operator Truth Table
Increment and Decrement Operator
Operator Meaning
++ Increment operator; increase the value of a
numeric variable or array element by 1
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
Bitwise Operator
X=10 00001010 00001010 00001010 00001010
& | ~ <<
y=25 00011001 00011001 2
11110101
00001000 00011011
00101000
z=8 z=27 z=-11
z=40
11110101 00001010 0X ff ff ff ff ff
00001010 X=-11 X=10
X=10 >> >> >>> X=-1 >>>
2 2 2 42
www.tutorialspoint.com/java
www.javatpoints.com