Java Language Basics By . Keywords Keywords of Java are given below – abstract continue for new...

28
Java Language Basics By www.PPTSWorld.com

Transcript of Java Language Basics By . Keywords Keywords of Java are given below – abstract continue for new...

Page 1: Java Language Basics By . Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.

Java

Language BasicsBy www.PPTSWorld.com

Page 2: Java Language Basics By . Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.

KeywordsKeywords of Java are given below –abstract continue for new switchassert*** default goto* package

synchronized boolean do if private this break double implements protected throw byte else import public throws case enum**** instanceof return transientcatch extends int short

try char final interface static void class finally long strictfp** volatile const* float native super while

•  * not used • **  added in 1.2 • ***   added in 1.4 • ****   added in 5.0

Page 3: Java Language Basics By . Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.

Identifiers User defined names of variables,

constants, arrays, functions, classes etc.

Identifiers can be defined using the combination of alphabets (a – z or A – Z) numbers (0 – 9) and underscore ( _ ).

Page 4: Java Language Basics By . Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.

Rules for naming Identifiers Any keyword should not be used as an

identifier. The name should consist of only

alphabets, numbers and underscore. First character should be any alphabet or

underscore. The name cannot start with a number. Lowercase and uppercase characters are

considered as different in C++.

Page 5: Java Language Basics By . Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.

Basic data TypesIn Java data types can be classified

under four major categories depending upon the space required for storing data in the memory.

Integers Floating Point Numbers Characters Boolean

Page 6: Java Language Basics By . Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.

Integers Integers are used to store whole

valued numbers. All of these are signed, positive or

negative values. Java does not support unsigned

integers. Java defines four integer types – byte,

short, int and long.

Page 7: Java Language Basics By . Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.

Integers

Data

Type

Size in

Bytes

Range

byte 1 -128 to 127

short 2 -32768 to 32767

int 4 -2,147,483,648 to 2,147,483,647

Long 8 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Page 8: Java Language Basics By . Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.

Floating Point Numbers

Floating point numbers are also known as real numbers and are used to store fractional precision values.

Java defines two types of floating point types :

float double

Page 9: Java Language Basics By . Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.

Floating Point Numbers

Data

Type

Size in

Bytes

Range

float 4 1.4e-045 to 3.4e+038

double 8 4.9e-324 to 1.8e+308

Page 10: Java Language Basics By . Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.

Characters char data type is used to store characters. char is a 16 bit type because Java uses

Unicode to represent characters instead of ASCII.

Unicode is a international character set having representation of all the character set found in the dozens of languages like English, Latin, Greek, etc. Therefore to store a character value in Java we require 2 bytes (16 bits). The range of a character set is 0 to 65,536.

Page 11: Java Language Basics By . Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.

Booleans Booleans are used to store logical

values, i.e. true or false. Boolean data types are used to store the

values returned by the conditional expressions, such as

i >= j.

Page 12: Java Language Basics By . Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.

OperatorsOperators are used to perform any

arithmetic or logical operations on given expressions.

The variables, constants, or values are joined to build an expression.

The variables or constants on which operator is applied are known as operands.

An operator is applied to one or more than one expression or operands to get some value.

Page 13: Java Language Basics By . Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.

Unary vs Binary OperatorSome operators need one operand and some need

two to operate. Unary operators require one operand to yield result, while binary operators require two operands to operate.

For example, in the following expression –40 – 20

We are using (–) as a binary operator because it is operating on two operands, 40 and 20.

Let’s take another expression – – 15

Here (–) is used as unary operator because it is working on only one operand.

Page 14: Java Language Basics By . Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.

Java OperatorsIn Java, operators can be divided into

following categories – Increment and Decrement Operator Assignment Operator Arithmetic operators Relational Operators Logical Operators

Page 15: Java Language Basics By . Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.

Increment / Decrement Operator

The unary operator ++ and -- are used to increment or decrement the value of any variable by 1.

++ is the increment operator and –– is the decrement operator.

These operators may be used either as prefix or postfix notations.

In prefix notation, the value of the variable is increased by 1 before the variable is used.

In the postfix notation, the value of the variable is increased by one after using the variable.

Page 16: Java Language Basics By . Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.

Increment / Decrement OperatorFor example, if i = 5, then in the following

expressions,j = i++k = ++i

In the first expression, the value of i will be assigned to j and then incremented, i.e.

j = 5 and i = 5. In the second expression, the value of i will

be incremented first and then this value will be assigned to k i.e.

i =5 and k = 5.

Page 17: Java Language Basics By . Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.

Assignment OperatorAssignment operator is used to assign a

value to any variable. In Java, “=” sign is used as assignment operator.

For example, in the following expression – a = 10

We can assign a single value to more than one variable in a single expression – a = b = c = 10;

Here, all the three variables, i.e. a, b and c are assigned the value 10.

Page 18: Java Language Basics By . Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.

Assignment OperatorIn Java, we should also assign a value to a

variable using compound assignment operator. For example,

a = a + 10;Above expression can also be written as,

a += 10;Here, += is a compound assignment

operator.

Page 19: Java Language Basics By . Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.

Assignment OperatorSimilarly we should used following

compound operators – a - = 10; a * = 10; a / = 10; a % = 10;

Page 20: Java Language Basics By . Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.

Arithmetic Operator

Arithmetic operators are used to perform numeric calculations on the operands.

There are five arithmetic operators –

Operator Description Example

+ Addition a + b

- Subtraction a – b

* Multiplication a * b

/ Division a / b

% Modulus (returns the remainder after division) a % b

Page 21: Java Language Basics By . Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.

Example:// Program to display the arithmetic operationspublic class Calc {

public static void main(String args[]) { int a = 5, b =7, result = 0; result = a + b; System.out.println(“Sum = ” + result); result = a - b; System.out.println(“Difference = ” + result); result = a * b; System.out.println(“Dividend = ” + result); result = a / b; System.out.println(“Remainder = ” + result);

}}

Page 22: Java Language Basics By . Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.

Relational OperatorsRelational operators are used to compare

two values in the given expression to see if they are equal or not. An expression containing any relational operator is called relational expression and they produce 0 or 1 as result. If the given expression is true it returns 1 and if the expression is false then it return 0.

Page 23: Java Language Basics By . Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.

Relational Operators

Operator Description Example

< Less than a < b

> Greater than a > b

= = Equal to a = = b

! = Not equal to a ! = b

<= Less than or equal to a <= b

>= Greater than or equal to a >= b

Page 24: Java Language Basics By . Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.

Logical Operators

Logical operators are used to combine and evaluate two or more expressions. These operators produce 0 or 1 as result.

The logical operator table is given below –

Operator Description Example

&& AND (a = = 5) && (b > a)

|| OR (a = = 5) || (b > a)

! NOT ! (a = = 5)

Page 25: Java Language Basics By . Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.

Expressions

Combination of operands and operators

Arithmetic expression a = b + 10; Relational expression a <= b Logical expression a<=8 && b>=8

Page 26: Java Language Basics By . Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.

Type Casting Type Casting  refers to changing an entity of one data type into another. This is important for the type conversion in developing any application. If you will store a int value into a byte variable directly, this will be illegal operation. For storing your calculated int value in a byte variable you will have to change the type of resultant data which has to be stored.

Page 27: Java Language Basics By . Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Page 28: Java Language Basics By . Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.

Example:// Program to display Type Castingimport java.io.*;public class Type{ public static void main(String args[]) { int a=10, f=65; byte b, e=65; char c='A'; b = (byte) a; // ------------------ from int to byte a = (int) c; // ------------------ from char to int char d = (char) f; // ------------------ from int to char System.out.println(b); System.out.println(a); System.out.println(d); }}