Vous êtes sur la page 1sur 20

Naming Conventions in JAVA

Naming Conventions provide rules to be followed by a Java Programmer while writing the names of packages, classes, methods, etc. A package represents a sub directory that contains a group of classes and interfaces. Names of packages in Java are written in small letters as: Java.awt Java.io Javax.swing A class is a model for creating objects. A class specifies the properties and actions of objects. An interface is also similar to a class. Each word of class names and interface names start with a capital letter as: String DataInputStream ActionListener A class and an interface contain methods and variables. The first word of a method name is in small letters, then from second word onwards, each new word starts with a capital letter as: println() readLine() getNumberInstance()

The naming convention for variables names is same as that for methods as: Age empName Employee_Net_Sal Constants represent fixed values cannot be altered. For example, PI is a constant whose value is 22/7 or 3.14159, which is fixed. Such constants be written by using all capital letters as: PI MAX_VALUE Font.BOLD Here, BOLD is a constant in Font class. (inbuilt constants) All Keywords should be written by using all small letters as: public void static

Data Types in JAVA


Integer Data Types Data Type Byte Short Int Long Example, byte rno=10; long x=150L; Float Data Types Data Type Float Double Memory Size 4 bytes 8 bytes Minimum and Maximum values -3.4e38 to -1.4e-45 (-ve values), 1.4e38 to 3.4e38(+ve values) -1.8e308 to -4.9e-324(-ve values), 4.9e-324 to 1.8e308 (+ve values) Memory Size 1 byte 2 bytes 4 bytes 8 bytes Minimum and Maximum values -128 to +127 (-28 to +28) -32768 to +32767 (-216 to +216) -2147483648 to +2147483647 (-232 to +232) -9223372036854775808 to +9223372036854775807 (-264 to +264)

Note: Float can represent up to 7 digits after decimal point but double up to 15 digits. Example, Float pi=3.142F; (if F is not taken then by default it allots 8 bytes for pi, as by default double is taken) Double distance=1.98e8 (e or E indicates * 10 to the power)

Character Data Type Data Type Char Example, char ch=X; Unicode System is an encoding standard that provides a unique number for every character, no matter what the platform, program, or language is. Unicode uses 2 bytes to represent a single character. String Data Types A String represents a group of characters like New Delhi, AP123, etc. The simplest way to create a String is by storing a group of characters into a String type variable is String str=New Delhi Boolean Data Types Boolean data types represent any of the two values-true or false. JVM uses 1 bit to represent a boolean value internally, for example Boolean response=true; (dont enclose boolean-true or false with quotation marks) Memory Size 2 bytes Minimum and Maximum Values 0 to 65535 (- 216 to + 216)

Literals: A literal represents a value that is stored into a variable directly in the program. boolean result = false; char gender = M; short s = 10000; int i = -1256; Note: the right hand side values are called literals because these values are being stored into the variables. As the data type of the variable changes, the type of the literal also changes. So we have different types of literals. Integer literals Float literals Character literals String literals Boolean literals

Operator in JAVA
An operator is a symbol that performs an operation. An operator acts on some variables, called operands to get the desired result. Example, a + b (a and b are operands, + is operator) Note: If an operator acts on single variable, it is called unary operator; if it acts on two variables, it is called binary operator; and if it acts on three variable, then it is called ternary operator. This is one type of classification. Arithmetic Operators (+, -, *, /, % (gives the remainder of division)) Example, String s1 = Wel; String s2 = come; String s3 = s1 + s2; Unary Operators Unary minus operator ( - ) Increment operator ( ++ ) Decrement operator ( -- ) Assignment Operator ( = ) stores a value into a variable. int x = y; int x = y + z -4; x + y = 10; //Invalid, because dont know where to store the value 15 = x; //Invalid

Expanded Notation ( x = x + 10;) Compact Notation ( x += 10; ) (addition assignment operator) Relational Operators: These operators are used for the purpose of comparing. ( >, >=, <, <=, ==, != ) The main use of relational operators is in the construction of conditions in statements, if( condition_is_true ) statement_to_be_executed. This statement could be applied in a program as if( a > b) System.out.println(a); if( a == 100) System.out.println(a value equals to 100); (= operator is to store the value into the variable) ( == operator is used to compare the two quantities) Logical Operators: These operators are used to construct compound conditions. A compound condition is a combination of several simple conditions. && (and) operator || (or) operator ! (not) operator Examples, if( a == 1 || b == 1 || c == 1) System.out.println(Yes); if(x > y && y < z) System.out.println(Hello); if(!(str1.equals(str2)) System.out.println(Not equal);

Boolean Operators: These operators act on boolean variables and produce boolean type result. & boolean (and) operator | boolean (or) operator ! boolean (not) operator Boolean & operator returns true if both the variables are true. Boolean | operator returns true if any one of the variables is true. Boolean ! Operator converts true to false and vice versa. Example, boolean a, b; // declare two boolean type variables a = true; //store boolean value true into a b = false; //store boolean value false into b a & b; // returns false, & gives true only if both are true a | b; //returns true, | gives true if any one is true a & a; //returns true since both are false b | b; //returns false since both are false !a; //gives false !b; //gives true

Bitwise Operators There are 7 bitwise operators in Java. These operators act on individual bits (0 and 1) of the operands. They act on only integer data types i.e. byte, short, long, and int. In case of these operators, the internal representation of numbers will be in the form of binary number system, which is different from the decimal number system used in our daily life. Binary number system uses only 2 digits-0 and 1 to represent any number. On the other hand, decimal number system uses 10 digits from 0 to 9.

Example, Converting 45 into binary number system. Converting binary number, 0010 1101 to decimal. Note: A negative number is represented as 2s complement of a positive number. Example, 10 (+ve decimal number) (binary 10 is 0000 1010 (obtain 1s complement for it by converting the bit 0 as 1 and vice versa, we get 1111 0101, add 1 to this, we get 2s complement form as 1111 0110 i.e. -10))
Note: Positive numbers are represented in binary using 1 complement notation and negative numbers are represented by using 2s complement notation.

Bitwise Operators (7 Types) Bitwise Complement Operator ( ~ ) (int x = 10, then ~x = -11) Bitwise and Operator ( & ) (x = 10, y = 11, then x & y) X x = 10 = 0000 1010 0 y = 11 = 0000 1011 0 ------------------------1 x & y = 0000 1010 = (10) 1 Bitwise or Operator ( | ) (x = 10, y = 11, then x | y) x = 10 = 0000 1010 y = 11 = 0000 1011 ------------------------x & y = 0000 1011 = (11) X 0 0 1 1

Y 0 1 0 1 Y 0 1 0 1

X&Y 0 0 0 1 X|Y 0 1 1 1 OR gate AND gate

Bitwise xor (exclusive or) Operator ( ^ ) (cap or carat or circumflex) x = 10 = 0000 1010 y = 11 = 0000 1011 ------------------------x & y = 0000 0001 = (1)

XOR gate

Bitwise Left Shift Operator ( << ) (int x = 10, then x << 2) This operator shifts the bits of the number towards left a specified number of positions. Bitwise Right Shift Operator ( >> ) (int x = 10, then x >> 2) This operator shifts the bits of the number towards right a specified number positions. Bitwise Zero Fill Right Shift Operator ( >>> ) This operator also shifts the bits of the number towards right a specified number of positions. But, it stores 0 in the sign bit. If we apply >>> on a positive number, it gives same output as that of >>. But in case of negative numbers, the output will be positive, since the sign bit is replaced by a 0.

Ternary Operator or Conditional Operator ( ?: ) (It acts on 3 variables so ternary) Syntax: variable = expression1? expression2 : expression3; if( expression1 is true) variable = expression2; else variable = expression3; Example, max = (a > b) ? a : b; Here, (a > b) is evaluated first. If it is true, then the value of a is stored into the variable max, else the value of b is stored into max.

Member Operator ( . ) (dot operator) This operator tells about member of a package or a class. It is used in three ways, Syntax: (Package contains classes, to refer to the class of the package) packagename.Classname; Example, java.io.BufferedReader //BufferedReader is a class in the package java.io Syntax: (class contains variables or methods, to refer to the variables of a class) Classname.variablename; (or) objectname.variablename; Example, System.out //out is a static variable in System class emp.id //id is a variable in Employee Class, emp is Employee Class Object Syntax: (class also contains methods, to refer to the methods of a class) Classname.methodname; (or) Objectname.methodname Example, Math.sqrt() //sqrt() is a method in Math class br.read() //read() is a method in BufferedReader class: br is object of //BufferedReader Class.

instanceof Operator (Instance means Object) This operator is used to test if an object belongs to a class or not. This operator can also be used to check if an object belongs to an interface or not. Syntax: boolean variable = object instanceof class; boolean variable = object instanceof interface; Example, boolean x = emp instanceof Employee; Testing that whether emp is an object of Employee Class or not. If emp is an object of then true will be returned into x, otherwise x will contain false. Employee class,

new Operator
Syntax: Classname obj = new Classname; Example,

Employee emp = new Employee();

//emp is an object of the Employee Class

Cast Operator Cast Operator is used to convert one data type into another data type. This operator can be use by writing data type inside simple braces. double x = 10.54; int y = x; //error because data types of x and y are different int y = (int)x; //here, x data type is converted into type and then stored into y (int) is called Cast Operator. Cast Operator is generally used before a variable or before a method. Priority of Operators First, the contents inside the braces: 0 and || will be executed. Next, ++ and --. Next, *,/, and % will execute. + and will come next Relational Operators are executed next. Boolean and Bitwise Operators. Logical Operators will come afterwards. Then Ternary Operator. Assignment Operators are executed at the last

Control Statements in JAVA


The two most commonly used statements in any programming language are: Sequential Statements: These are the statements which are executed one by one. Control Statements: These are the statements that are executed randomly and repeatedly. Example, System.out.println(Hello, this is Ravi); x = y + z; System.out.println(x); These statements are executed by JVM one by one in a sequential manner. Control Statements are the statements which alter the flow of execution and provide better control to the programmer on the flow on the flow of execution. They are useful to write better and complex programs.

if else statement

dowhile loop while loop for loop for-each loop switch statement break statement continue statement return statement

A statement represents a single time execution from top to bottom and a loop represents repeated execution of several statements.

ifelse Statement Syntax: if(condition) statements1; [else statements2;] Different Variations for ifelse Statement if(Condition1) statements1; else if(Condition2) statements2; else if(Condition3) statements3; else statements4; (or) if(Condition1) if(Condition2) if(Condition3) statements1; else statements2; else statements3; else statements4;

dowhile loop Syntax: do{ statements; }while(Condition); Note: No need to use { and } braces, if there is only one statement inside the dowhile loop. while loop Syntax: while(Condition) { statements; } Note: In a dowhile loop, the statements are executed without testing the condition, the first time. From the second time only the condition is observed. This means that the programmer does not have control right from beginning of its execution. In a while loop, the condition is tested first and then only the statements are executed. This means it provides better control right from the beginning. Hence, while loop is more efficient than dowhile loop.

for loop The for loop is also same as dowhile loop or while loop, but it is more compact syntactically. The for loop executes a group of statements as long as a condition is true. Syntax: for( expression1; expression2; expression3) (initialization, condition, modifying) { statements; } Example, for(int x=1; x<=10; x++) { System.out.println(x); } Note: dowhile or while loops are used when we do not know how many times we have to execute the statements. It just depends on the condition. The execution should continue till the condition is false. But, the for loop is more suitable for situations where the statements should be executed a fixed number of times. Here, we know how many times exactly we want to execute.

Vous aimerez peut-être aussi