Std: 12th Science
English Medium
Subject: Computer
Java Basics
Chapter-7
Introduction to Java
main()
{
…
}
Source code
object code
Compiler
Interpreter
Java Platform-independent
Java
Source
Java
Compiler
Java
Bytecode
Java
Interpreter
For Linux
Java
Interpreter
For windows
Java
Interpreter
For Max
Java
Source
Java
Compiler
Java
Bytecode
Java
Interpreter
For Linux
Java
Interpreter
For windows
Java
Interpreter
For Max
Creating Simple Java Application
Creating Simple Java Application
//**
* First program to calculate cost */
public class CallCost
{
public static void main(String[] args)
{
/* Declare variable*/
double balance;
double rate;
double duration;
double cost;
balance = 170;
rate = 1.03;
duration = 37;
Cost = duration * rate;
balance = balance – cost;
System.out.print(“Call Duration:”)
System.out.print(duration);
System.out.print(“Seconds”);
System.out.println(“Balance: “ + balance + “Rs”);
}
}
Program Illustration
Structure of a Java Program
public static <class-name>
{
<Optional-variable-declarations-and-methods>
public static void main(String[] args)
{
<Statements>
}
<Optional-variable-declarations-and-methods>
}
< and > is used as placeholder that describes something actual we need to type while writing actual program.
Important terms
Data types
Variables
Literals
Comments
Java Statements and expressions
Arithmetic Operators
Comparison Operators
Logical Operators
Data Types
Data type determines the required memory size, type of value, range of values and type of operations that can be performed.
Java supports 8 primitive data types that handle common type of integers, floating point numbers, characters and Boolean values. (true or false)
These types are : byte, short, int, long, float, double, char, boolean.
The first four types hold integer values, next two hold real values, character holds single character from unicode character set and boolean holds either true or false.
Data Types
Data type | Storage space | Type of value | Range of values | Default value |
byte | 1 byte | Integer | -128 to 127 | 0 |
short | 2 bytes | Integer | -32767 to 32768 | 0 |
int | 4 bytes | Integer | -2147483648 to 2147483647 | 0 |
long | 8 bytes | Integer | -9223372036854775808 to 9223372036854775807 | 0 |
float | 4 bytes | Real | 10+38 with about 7 Significant digits | 0 |
double | 8 bytes | Real | 10+308 with about 7 Significant digits | 0 |
char | 2 bytes | Character | 16-bit Unicode Character | 0 |
boolean | 1 byte | Boolean | True, False | False |
Integer Numbers:
Integer numbers with b bits precision store signed values in the range of : -2b-1 to 2b-1 -1
For eg. If b = 8 then
= -(2b-1)
= -(28-1)
= -(27)
= -(2*2*2*2*2*2*2)
= -128
Range: -128 to 127
For eg. If b = 16 then
= -(2b-1)
= -(216-1)
= -(215)
= -128
Range: -32768 to 32767
For unsigned values: (0 to 2b-1-1)
For eg. If b = 8 then
= (2b-1)
= (28-1)
= (27)
= (2*2*2*2*2*2*2)
= 128
Range: 0 to 127
Real numbers in Java are compliant with IEEE 745 an international standard.
IEEE means Institute of Electrical and Electronic Engineers.
Java uses Unicode character set.
The char type has 16 bits of precision and is unsigned.
Unocide allows thousands of characters from many different languages and different alphabets
To store data ASCII-7 was used in the beginning.
ASCII-8 was introduced to increase character set.
Multimedia computers now-a-days use Unicode system.
Boolean is not a number, nor can it be tread as one.
It contains either true or false.
Variables:
If we want anything to be remembered by the computer during the program execution, then it needs to be stored in the memory of a computer.
In short variables get stored in RAM and Files get stored in Hard Disk.
A name used to refer to the data stored in memory is called a variable. It can store a piece of data.
<type-name> (variable-names)
For eg. int marks; 🡪 marks is variable, int is data type
Float rate;
Double interest=12.5;
Variable name rules:
Variable name must begin with an alphabet, Underscore(_) or dollar($).
After first character, it may contain digits, alphabets, $ and Underscore character (_)
No space are allowed in variable name.
Legal variable names: birth_date, result, amount$
Illegal names: 4me, %discount
It con not be on the name of reserved words like int, if.
When a variable name includes several words, capitalize the first alphabet of each word, except the first word.
For eg. balanceAmount, birthDate.
This is known as camel case.
Literals
A name used for a constant value is known as Literal.
There are some different kinds of literals
Numeric Literals
Character Literals
String Literals
Boolean Literal
Numeric Literals
Tow types of numeric literals: (1) Integer (2) Real
Integer Literal:
(1) Decimal Literal: For ex: 345 , -98
A decimal literal larger than int is automatically of type long.
We can force a smaller number to be a long by appending an L or l as a suffix. For ex: 4L.
(2) Octal Literal:
Octal numbers are from 0 to 7.
In java a numeric literal with a leading 0(zero) is interpreted as an octal number. For ex: 045.
Numeric Literals
Integer Literal:
(3) Hexa Decimal Literal:
Hexadecimal numbers are from 0 to 9 and A to F.
A to F represents 10 to 15 respectively.
In java a numeric literal with a leading 0x or 0X is interpreted as an Hexadecimal number. For ex: 0xA67.
(4) Binary Literal:
Java also supports binary numbers, using the 0 and 1 with the prefix 0b or 0B.
For ex: 0b10101
Numeric Literals
Real number literals:
Real number literals are also called floating point literals.
These numbers can be represented using two types of notations: Standard and Scientific
In scientific notation, a number is followed by letter e or E and a signed integer exponent.
For ex: 1.2e15
Operators
Arithmetic Operators
Unary Operators
Unary Operators
Comparison Operators
Logical Operators
Short circuiting
Conditional Operator
Assignment Operator
Shorthand assignment operator
Assignment Shorthand Assignment
Typecast
Precedence Order
Associativity
Operations | Operators | Associativity |
Unary operations | ++, --, type cast | Right-to-left |
Multiplication, Division, Modulus | *, ?, % | Left-to-Right |
Addition , Subtraction | + , - | Left-to-Right |
Relational operators | < , > , <=, >= | Left-to-Right |
Relational operators | == , != | Left-to-Right |
Logical AND | && | Left-to-Right |
Logical OR | || | Left-to-Right |
Conditional Operator | ?: | Right-to-left |
Assignment Operator | = , +=, -=, *=, /=, %= | Right-to-left |
Control structure
Branches
Block
Format:
{
<statements>
}
Purpose of block:
To group a sequence of statements into a unit that can be treated as single statement.
To group logically related statements.
To create variables with local scope for statements within a block.
To create variables with local scope.
Blk1:
{
int temp;
temp = x;
x = y;
y = temp;
}
A block can also be labeled by a name. For ex: blk1 here.
Variable conflict
Understanding variable’s scope
if statement
Structure of if statement:
If(<boolean-expression>)
{
<stetement-1>;
}
else
{
<statement-2>;
}
When if statement is executed, it first evaluates boolean expression.
If its value is true, it executes statement-1.
Otherwise it executes statement-2 written after else.
There can be several statements
if statement