Lecture 9 - Java Basics
Lecture 9 - Java Basics
Lecture 9 - Java Basics
programming
Lecture 9
1
Outline
• Java input/output
• Variables
• Literals
• Type Casting
• Issues in Casting
2
Hello World!
public class HelloWorld
{
public static void main(String[] args) {
System.out.println("Hello World");
}
}
3
public class HelloWorld{
● Every java application begins with a class definition. In the program,
HelloWorld is the name of the class.
public static void main(String[] args) {
● Main method.
● Every application in Java must contain the main method.
● The Java compiler starts executing the code from the main method.
System.out.println("Hello World");
● It prints the text Hello, World! On console screen . The text inside the
quotation marks is called String in Java.
4
Java Output
● Display output to the user. There are 3 ways to display the output in Java.
System.out.println(“Hello World”);
System.out.print(“Hello World”);
System.out.printf(“Hello World”);
6
Java Input
● Import Scanner class to get input from the user using Scanner class object.
import java.util.Scanner;
● create an object of the Scanner class.
Scanner in = new Scanner (System.in);
Scanner - Java class
in -User defined name for the scanner class object
short 0 long 0L
char \u0000
10
public class Variables{
public static void main(String[] args) {
boolean flag = true;
int range = -4250000;
double number = -42.3;
float number = -42.3f;
char letter = '\u0051';
System.out.println(flag);
System.out.println(range);
System.out.println(number);
System.out.println(number);
System.out.println(letter);
} 11
String is a data type???
● Strings in Java are not primitive types (like int, char, etc).
● Instead, all strings are objects of a predefined class named String.
12
Variables
● A variable is a location in memory (storage area) to hold data.
Variable Declaration
dataType variableName;
Variable Initialization
dataType variableName = value;
13
Variable Naming Conventions
● Java is case sensitive.
● Start with either a letter or an underscore, _ or a dollar, $ sign.
● Do not start with numbers.
● Do not use whitespace.
14
Types of variables in Java
15
Variables contd..
3. Local Variables
• methods store its temporary state
• only visible to the methods
• not accessible from the rest of the class
16
Comments
● Comments are a portion of the program that are completely ignored by
Java compilers.
● They are mainly used to help programmers to understand the code.
● In Java, there are two types of comments:
○ single-line comment - Comment starts and ends in the same line. // symbol is
used.
○ multi-line comment - /* and */ symbols are used.
17
Single line comment
Ex: //This is a single line comment
Multi-line comment
Ex:
/*
This is a multi line comment
in Java
*/
18
Literals
• There are five basic types of literals in Java.
• Integer literals
• Floating point literals
• Character literals
• String literals
• Boolean literals
19
Integral literals
• Literal: Any constant value which can be assigned to the variable is called
literal/constant.
20
Floating point literals
• Can be expressed even using E or e (for scientific notation)
double d1 = 123.4;
double d2 = 1.234e2; //same value as d1, but
in scientific notation
float f1 = 123.4f;
22
String Literals
• Any Unicode (UTF-16) character
“ ” “”
Backspace \b
Tab \t
Nonprinting characters,
Vertical tab \v
New line (line feed) \n
Always begins with a
Form feed \f backward slash
Carriage return \r
Quotation quote (“) \”
Single quote (‘) \’
Backslash (\) \\
24
Example: Quotation quote (“) - \”
25
Boolean Literals
• A Boolean type must be assigned the value of the constants true or
false.
[Meaning, these exact lowercase words.]
26
Scope of Variables
• Scope of a variable is the part of the program where the variable
is accessible.
variable n in Block2
is available only in Block2,
it goes out of the scope
at end of Block2
27
Scope of Variables contd….
Scopes of the variables args,
local2,local1 and local 3
28
Scope of Variables contd..
• The place of declaration decides the scope of the variable
• Instance & class variables declared inside a class
• Local variables declared & used inside methods
29
Symbolic Constants
• Named constant value defined once and used throughout a
program.
• Benefits
• Easy to modify the program
• Easy to understand the program
30
Syntax of Symbolic Constants
final type symbolic-name =value;
31
Symbolic Constants contd..
32
Mixing Data Types
float value1, value2, average;
value1 = 1.4f;
value2 = 2.6f;
average = (value1 + value2)/2;
33
Mixing Data Types contd..
float value1, value2, average;
value1 = 1.4f;
value2 = 2.6f;
average = (value1 + value2)/2.0;
34
Automatic Type Conversion
• The rule for mixed-type expressions is:
“The type of each value is converted to type of the higher type in the
expression”
35
Assigning Values
• An assignment is unacceptable if the result of the expression on
the right-hand side is of a higher type to that of the variable on the
left-hand side.
int i=4;
float f = 4.0f;
f = i;
i = f;
f = 10.0;
f = 10;
36
Type Casting
• Occasionally we want to force a calculation to take place against the
implicit type conversion rules
• we use a cast operator to achieve this:
- The value of the cast number must not be too large for the new type
38