Java

Language Basics

Keywords
Keywords of Java are given below –
abstract continue for switch assert*** default goto* synchronized boolean do if this break double implements byte else import case enum**** instanceof catch extends int char final interface class finally long const* float native
• •

new package private protected public return short static strictfp** super throw throws transient try void volatile while

 * not used **  added in 1.2

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

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

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

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.

Integers
Data Size in Type Bytes byte short int Long 1 2 4 8 -128 to 127 -32768 to 32767 -2,147,483,648 to 2,147,483,647 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 Range

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

Floating Point Numbers
Data Type float double Size in Bytes 4 8 Range 1.4e-045 to 3.4e+038 4.9e-324 to 1.8e+308

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.

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.

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.

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

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

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.

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.

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.

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.

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

Arithmetic Operator
Arithmetic operators are used to perform numeric calculations on the operands. There are five arithmetic operators –
Operator + * / % Addition Subtraction Multiplication Division Modulus (returns the remainder after division) Description Example a+b a–b a*b a/b a%b

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); } }

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.

Relational Operators
Operator < > == != <= >= Description Less than Greater than Equal to Not equal to Less than or equal to Example a<b a>b a==b a!=b a <= b

Greater than or equal to a >= b

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 Example below given Operator Description –
&& || ! AND OR NOT (a = = 5) && (b > a) (a = = 5) || (b > a) ! (a = = 5)

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

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

// 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); }

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.