You are on page 1of 10



Reference Material


 Example: A swipe machine encapsulates/hides internal circuitry from all users and provides simple interface for access by every user. C language uses structured programming.  In the above program data and methods are encapsulated in a class called Customer and data is hidden from user by declaring them to private and interface is allowed to setter and getter methods. The specialized ones automatically inherit all the properties of generalized ones. Assembly language uses non structured whereas java uses object oriented programming. Many versions evolved over the years now the current version is java JDK 8. Previously it was called OAK but it’s renamed to java during 1995.Unit . and polymorphism. The oop concepts or key attributes of oop are encapsulation. computer languages adopt particular programming methodology to write programs. Encapsulation:  Encapsulation is the mechanism of hiding the internal details and allowing a simple interface which ensures that the object can be used without having to know its internal details. For example.I        INTRODUCTION Java is an object oriented programming language developed by James Gosling & his crew. inheritance. . All java programs are written using object oriented programming concepts.  In java.  The following program demonstrates the concept of encapsulation. Inheritance:  Inheritance is a mechanism by which we can define generalized characteristics and behavior and also create specialized ones. KEY ATTRIBUTES OF OOP In computer science. encapsulation is achieved by binding data and methods in a single entity called class and hiding data behind methods and allowing the interface with the public methods of class.

 Inheritance can be achieved in java with the extends keyword. Name. LicenseNumber. In the above program the same move() operation is behaving differently in different situations. four-wheeler classes are specialized ones which inherit all the properties of generalized ones like ID. and two-wheeler. Vehicle is a generalized class with its own characteristics and behavior.  For example in the above diagram. Polymorphism:  The ability of an object/operation to behave differently in different situations is called polymorphism. SIMPLE PROGRAM .

Description about each part of a program:  The first line import the classes required for the program.print() method.  The statements written between /* */ are called multiline comments which will be ignored by compiler. System is a class and out is a variable in it.print(). In between { } we can write variables and methods.The process of getting the output for above program requires 3steps.  When we call out variable PrintStream class object will be created  Compile the program using javac command followed by Demo.  The keyword static allows main() to be executed without creating an object for class Demo.class file.  Since JVM calls the main() method there won’t be nothing for the main() to return to JVM. every java program is written within a class. Since the above program requires print() method of system class the package has to be imported into the program.  main() is the method which is the starting point for JVM to start execution.out.  To print a string to the output we use “ ”and passed as a parameter to print() in command prompt which generates . So it is declared as void type.  Execute the program using java command followed by Demo which will generates the output. It explains the operation of program to anyone reading the program. So we write System.  To print something to the console output we use System.  Since java is oop language. Otherwise it is not accessible to JVM. Print() method belongs to PrintStream class.  Since main() method should be available to JVM which is an outside program it should be declared with an access specifier public. So we use class keyword followed by class name Demo.out.  Enter the source code in either editor like notepad or IDE like netbeans or eclipse and save the program name as Demo. This is necessary because main() is executed by JVM before any objects are made.  A class code starts with a { and ends with }. The classes required will be in the subdirectory lang of main directory java. Elements of a Java Program .

or newline. Used to declare arrays.6 ‘X’ “This is a test”  Comments: There are three types of comments defined by Java. . variable. Used to terminate statements.  KEYWORDS: There are 50 reserved keywords currently defined in the Java language. here are some literals: 100 98. Single-line. tab. Period Contains parameters in method call & definition. Const and goto keywords are reserved and meant for future use. The documentation comment begins with a /** and ends with a */. Separates package names from sub packages. . here x is an identifier for a variable. Define block of code for class. class.  Separators: In Java. For example. We can’t start an identifier with a digit. multiline.We use one whitespace character between each token in java. class and method. Contains expressions in control statements. Used to initialize arrays. This helps to refer to that item from any place in the program. java reserves true. combined with the syntax of the operators and separators. The separators are shown in the following table: Symbo l () Name Description Parenthesis {} Braces [ ] . method and local scopes. documentation comment. or method. Separates identifiers in variable declaration. Surrounds cast type. Brackets Semicolon Comma . Documentation comment is used to produce an HTML file that documents the program.  IDENTIFIERS: Identifiers are the names given to a variable. form the definition of the Java language.  In addition to the keywords. These keywords. false and null. whitespace is a space. there are a few characters that are used as separators. Chains statements together in for statement. These keywords cannot be used as names for a variable. Whitespaces: In Java. Separate variable or method from ref.  Literals: A constant value in Java is created by using a literal representation of it. Ex: int x.

z=x/3+y.print(++x). Ex: int x=10.z. value).  Postincrement operator allows other operations to be performed first and then incrementation happens. z=y-x. z.z.y=3.y=3.  Categories of operators:  Arithmetic operators  Assignment operator  Unary operators  Relational operators  Logical operators or short circuit operators  Boolean operators  Bitwise operators  Ternary or conditional operator  Member operator  Instanceof operator  New operator  Cast operator.out. a and b are operands ‘+’ is an operator meant for addition operation. z=y/x.s3=”two”.print(-x)). by 1.print(x++). System. by 1. / Division Int x=4.out.  Preincrement operator allows incrementation first and then other operations are performed. System. -decrements a variable value Int x=5.z. z= x.out. % Modulus operator Int x=4. System.print(x++).y=3.out.// gives result 11 because incrementation first happens & then print operation happens. z=y*x.  Unary operators: Operator Operation Example Unary minus(negates given Int x=5.y=3. Ex: a + b.print(x--).// gives result 10 because print operation happens . z=y%x. String s2=”one”. ++ Increments a variable value Int x=5. Ex: int x=3. System. y=5. (gives remainder)  Assignment operator: This is used to store a value or variable or an expression into a variable. string 12+10.z.  Arithmetic operators: Operator Operation example + Addition. concatenation String s1=s2+s3.out. Ex: int x=10. * Multiplication Int x=4. System. Subtraction Int x=4.OPERATORS  An operator is a symbol which tells the compiler to perform a particular operation on operands.

Ex: int x=10.print(2>3).print(2<3).  Logical Operators: This is used to construct compound conditions.print(a>b||b<c).out.out. element greater than other. evaluates to false otherwise true returns. element greater than or equal to other.out.first incrementation then happens. otherwise false return. simple conditions evaluates to true otherwise false return. !(not operator) Evaluates to true if condition System.print(2==3). element lesser than other element. <=(lesser than or equal to) Evaluates to true if System.otherwise false.out. System. != (not equal to) Evaluates to true if both System.out. otherwise false. Ex: int x=10. otherwise false. element lesser than or equal to other element .print(2<=3).print(2!=3).print(a>b&& condition evaluates to true b<c).out.  Postdecrement operator allows other operations to be performed first and then decrementation happens.out. Operator Operation example >(greater than) Evaluates to true if System.print(x--). ==(equal to) Evaluates to true if both System.   Boolean operators: .print(2>=3).// gives result 9 because decrementation first happens & then print operation happens.out. elements are equal. Operator Operation Example &&(and operator) Evaluates to true if each simple System. ||(or operator) Evaluates to true if any one of System.out. Predecrement operator allows decrementation first and then other operations are performed.  Relational Operators: These are used to perform comparison between elements. Otherwise false >=(greater than or equal to) Evaluates to true if System.out. elements are equal. Otherwise false.out. Otherwise false <(lesser than) Evaluates to true if System. System.print(!(a>b)).// gives result 10 because print operation happens first decrementation then happens.print(--x).

out  Used when refer to method of a class or object. ^(Bitwise xor Operator) Gives 1 if odd System. Otherwise false returns.out. >>>(Bitwise zero fill right shift Shifts bits to the right operator) with the specified number of times and fill left side empty places with zeroes.out.sqrt(23).  Used when refer to subpackage and class of a package. >>(Bitwise right shift operator) Shifts bits to the right System.) This is used in 3ways.//returns with the specified 00000010 number of times.  Used when refer to variable of a class or object.//returns are 1.//returns number of 1s present 00000001 in input.print(x|y). <<(Bitwise left shift operator) Shifts bits to the left System. Syntax: variable =expression1? expression2:expression3.out.These operators act on Boolean variables and produce Boolean type result. Example: System. Otherwise exp3 will be executed. Example: Math.JOptionPane. Otherwise 00010100 false returns. Otherwise false returns.  Member operator (.print(x<<2).//returns with the specified 00101000 number of times.//returns bits are 1. Operators &(Boolean and operator) |(Boolean or operator) !(Boolean not operator) Operation Returns true if both variables true.print(a|b)//returns true System.print(x&y).swing.print(x^y).out.print(x>>2). Converts true to false and vice-versa. Returns true if any variable is true. Operators Operation Example x=10. .out. Example: max = (a>b)? a :b.out.out.out.print(a&b)//returns false System. Otherwise false returns. Example: import javax. If exp1 evaluates to true exp2 will be executed.print(!(a|b))//returns false  Bitwise operators: These operators act on individual bits of a number and produce the appropriate result. Example(a=true b=false) System.  Ternary Operator or Conditional Operator This acts an an alternative for if else statement. Otherwise false 00001010 returns |(Bitwise or Operator) Gives 1 if any of the System. y=11 ~(Bitwise Complement Operator) Gives complement of (~x)=-11 a given number & (Bitwise and Operator ) Gives 1 if both bits System.

th 5 Relational operators th 6 Boolean and bitwise operators 7th Logical operators th 8 Ternary operator th 9 Assignment operator DATATYPES Datatype is an identified type for any data being used in a program. array. To determine which operators execute first priority will be assigned to operators.  Primitive datatypes  Non primitive datatypes Under primitive datatypes we have 8 datatypes.5678. Example: customer custobj=new customer(). Syntax: datatype target-var=(target-datatype)variable. Syntax: classname obj=new classname().  Cast operator Cast operator is used to convert one data type into another data type. Example: int x. . priority operators st 1 () . x=(int)y. Under non primitive we have class. % 4th +. instanceof Operator This is used to test if an object belongs to a (class or interface) or not. This is used to determine the following. float y=24. [] nd 2 ++ . In java datatypes are divided into 2 categories. interface. Syntax: Boolean variable= object instanceof class. o Type of data stored in a variable o No of bytes it occupies in memory o Range of data. /. Example: Boolean x= custobj instanceof customer.  Priority of operators In an expression some of the operators will execute first and some operators will execute next. -rd 3 *.  New operator new operator is often used to create objects to classes. The priority is as follows.

45f. The type of data stored in the variables can be specified with datatype.(+231-1) long 8bytes (-263). Example: int x=23456.of bytes Min-max value occupied byte 1byte (-27). double d=3. whereas double can represent upto 15 digits accurately after decimal point. . Category Datatype No.4e+038) Floating type double 8bytes (3. boolean b=true.4e-038).(3. char datatype can support 65536 characters including all human language characters which is there in the Unicode system. short s=23451. int i=2345666. It holds data in a program It is named location in memory whose value changes during program execution.0f 0. long l=342156789.(+27-1) short 2bytes (-215). Float f=3.(+215-1) Integer type int 4bytes (-231).4e+308) Textual type char 2bytes 1. VARIABLE A variable is an identifier for the data in the program. Syntax: datatype variable-name=value. char ch=’x’.The following table shows various datatypes with their details. Float can represent upto 7 digits accurately after decimal point.65535 Logical type boolean 1bit       Default value 0 0 0 0 0.2134d.4e-308).(+263-1) float 4bytes (3.(3.0d Null false example byte b=10.