Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Language Basics By www.PPTSWorld.com. Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.

Similar presentations


Presentation on theme: "Java Language Basics By www.PPTSWorld.com. Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package."— Presentation transcript:

1 Java Language Basics By www.PPTSWorld.com

2 Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package synchronized boolean do if private this break double implements protected throw byte else import public throws case enum **** instanceof return transient catch 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

3 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 ( _ ).

4 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++.

5 Basic data Types In 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

6 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.

7 Integers Data Type Size in Bytes Range byte1-128 to 127 short2-32768 to 32767 int4-2,147,483,648 to 2,147,483,647 Long8-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

8 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

9 Floating Point Numbers Data Type Size in Bytes Range float41.4e-045 to 3.4e+038 double84.9e-324 to 1.8e+308

10 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.

11 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.

12 Operators Operators 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.

13 Unary vs Binary Operator Some 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.

14 Java Operators In Java, operators can be divided into following categories –  Increment and Decrement Operator  Assignment Operator  Arithmetic operators  Relational Operators  Logical Operators

15 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.

16 Increment / Decrement Operator For 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.

17 Assignment Operator Assignment 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.

18 Assignment Operator In 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.

19 Assignment Operator Similarly we should used following compound operators –  a - = 10;  a * = 10;  a / = 10;  a % = 10;

20 Arithmetic Operator Arithmetic operators are used to perform numeric calculations on the operands. There are five arithmetic operators – OperatorDescriptionExample +Additiona + b -Subtractiona – b *Multiplicationa * b /Divisiona / b %Modulus (returns the remainder after division)a % b

21 Example: // Program to display the arithmetic operations public 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); }

22 Relational Operators Relational 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.

23 Relational Operators OperatorDescriptionExample <Less thana < b >Greater thana > b = =Equal toa = = b ! =Not equal toa ! = b <=Less than or equal toa <= b >=Greater than or equal toa >= b

24 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 – OperatorDescriptionExample &&AND(a = = 5) && (b > a) ||OR(a = = 5) || (b > a) !NOT! (a = = 5)

25 Expressions Combination of operands and operators  Arithmetic expression a = b + 10;  Relational expression a <= b  Logical expression a =8

26 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.

27

28 Example: // Program to display Type Casting import 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); }


Download ppt "Java Language Basics By www.PPTSWorld.com. Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package."

Similar presentations


Ads by Google