2_Java Fundamentals
2_Java Fundamentals
By
Manjiri Tatke
Objectives
• After completing this , participants will be able to:
• Understand Basic Java Language constructs like:
• Keywords
• Primitive Data Types
• Operators
• Variables
• Literals
• Write Java programs using control structures
• Best Practices
Keywords in Java
Primitive Data Types
• Integral types (byte, short, int, and long)
• Floating point types (float and double)
• Textual type (char)
• Logical type (boolean)
Some New Integral Primitive Types
Type Length Range
–27 to 27 – 1
byte 8 bits (–128 to 127,
or 256 possible values)
–263 to 263 – 1
long (–9,223,372,036854,775,808 to
64 bits 9,223,372,036854,775,807, or
18,446,744,073,709,551,616 possible
values)
Floating Point Primitive Types
Type Float Length
float 32 bits
double 64 bits
(default type for floating
point literals)
Example:
public float pi = 3.141592F;
Textual Primitive Type
• The only primitive textual data type is char.
• It is used for a single character (16 bits).
• Example:
– public char colorCode = 'U';
Single quotes must be used with char literal values.
Java Language Trivia: Unicode
• Unicode is a standard character encoding system.
• It uses a 16-bit character set.
• It can store all the necessary characters from most languages.
• Programs can be written so they display the correct language for most countries.
Constants
• Variable (can change):
– double salesTax = 6.25;
• Constant (cannot change):
– final int NUMBER_OF_MONTHS = 12;
firstName = "Gary";
Variables
• Variables are data placeholders.
• Java is a strongly typed language, therefore every variable must have a declared type.
• The variables can be of two types:
• reference types: A variable of reference type provides a reference to an object.
• primitive types: A variable of primitive type holds a primitive.
• In addition to the data type, a Java variable also has a name or an identifier.
Stack
Primitive Variable
value
Reference Variable Heap
reference
• Assigning
String namethe
= value
name1;of one variable to another:
• Representing
total = quantityvalues within ;a mathematical expression:
* price
• Example:
System.out.println(message);
System.out.println(greet1 + " " + greet2 + "!");
Output:
Hello World!
Hello World!
int and double Values
• int variables hold whole number values between:
– –2,147,483,648
– 2,147,483,647
– Examples: 2, 1343387, 1_343_387
3 3.0
• 3 long num3;
• 4 num3 = num1 * num2; //num3 is 3703629630
Caution with Promotion
• Equation:
7 / 2 = 3.5
• Example of potential issue:
• 1 int num1 = 7;
• 2 int num2 = 2;
• 3 double num3;
• 4 num3 = num1 / num2; //num3 is 3.0
• Example of potential solution:
• 1 int num1 = 7;
Changed from int to double
• 2 double num2 = 2;
• 3 double num3;
• 4 num3 = num1 / num2; //num3 is 3.5
Type Casting
• When to cast:
– If you assign a larger type to a smaller type
byte short int long
• Examples of casting:
• int longToInt = (int)20L;
• short doubleToShort = (short)3.0;
Caution with Type Casting
Example of potential issue:
1 int myInt;
2 long myLong = 123987654321L;
3 myInt = (int) (myLong); // Number is "chopped“
4 // myInt is -566397263
• Type cast num1 and num2 as int types in the assignment line:
int num1 = (int)(1 + 2 + 3 + 4.0); //10
int num2 = (int)((1 + 2 + 3 + 4) * 1.0); //10
Floating Point Data Types and
Assignment
• Example of potential problem:
float float1 = 27.9; //compiler error
• Do – While Loop: Loop executes at least once even if the condition is false
do
{ //body of the loop
} while (condition)
Iteration Statements
• For Loop:
for( initialization ; condition ; iteration)
{ //body of the loop }