




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible.
Typology: Cheat Sheet
1 / 128
This page cannot be seen from the preview
Don't miss anything!





























































































(Affiliated to JNTUH, Hyderabad, Approved by AICTE - Accredited by NBA & NAAC – ‘A’ Grade - ISO 9001:2015 Certified) Maisammaguda, Dhulapally (Post Via. Hakimpet), Secunderabad – 500100, Telangana State, INDIA.
Objectives:
Java Programming- History of Java, comments, Data types, Variables, Constants, Scope and Lifetime of variables, Operators, Type conversion and casting, Enumerated types, Control flow- block scope, conditional statements, loops, break and continue statements, arraya,simple java stand alone programs,class,object and its methods, constructors and its types , methods, static fields and methods, access control, this reference, overloading methods and constructors, recursion, garbage collection, exploring string class.
UNIT – II Inheritance – Inheritance types, super keyword, preventing inheritance: final classes and methods. Polymorphism – method overloading and overriding, abstract classes and methods. Interfaces- Interfaces Vs Abstract classes, defining an interface, implement interfaces, accessing implementations through interface references, extending interface. Packages- Defining, creating and accessing a package, importing packages.
Exception handling- Define Exception, advantages of exception handling, the classification of exceptions- exception hierarchy, checked exceptions and unchecked exceptions, usage of try, catch, throw, throws and finally, creating own exception sub classes. Multithreading – Define Thread, multithreading, thread life cycle, creating threads, interrupting threads, thread priorities, synchronizing threads, inter-thread communication, producer consumer problem.
Collection Framework in Java – Introduction to java collections, Overview of java collection framework, commonly used collection classes- Array List, Vector, Hash table, Stack, String tokenizer. Files- Streams - Byte streams, Character streams, Text input/output, Binary input/output, random access file operations, File management using File class. Connecting to Database – JDBC Type 1 to 4 drivers, connecting to a database, querying a database and processing the results, updating data with JDBC.
S. No Unit Topic Page no
I OOP Concepts:-^ Data abstraction, encapsulation inheritance^
Benefits of Inheritance 2
3 I Polymorphism, classes and objects 2
4 I Procedural and object oriented programming paradigms 3
5 I Java Programming-^ History of Java^4
6 I Comments, Data types, Variables, Constants 5-
7 I Scope and Lifetime of variables 10
8 I (^) Operators, Operator Hierarchy, Expressions 11-
9 I Type conversion and casting, Enumerated types 12-
Control flow- block scope, conditional statements, loops, break and continue statements
11 I Simple java stand alone programs, arrays 14-
12 I Console input and output, formatting output 18-
13 I Constructors, methods, parameter passing 19-
14 I (^) Static fields and methods, access control, this reference, 21-
Overloading methods and constructors, recursion, garbage collection,
Building strings, exploring string class. (^) 34-
S. No Unit Topic Page no
Inheritance – Inheritance hierarchies super and sub classes, Member access rules
super keyword, preventing inheritance: final classes and methods, the Object class and its methods.
19 II^ Polymorphism – dynamic binding, method overriding, 41-
20 II (^) abstract classes and methods. 43
Interfaces- Interfaces Vs Abstract classes, defining an interface, implement interfaces
Accessing implementations through interface references, extending interface.
23 II Inner classes- Uses of inner classes, local inner classes 45-
24 II^ Anonymous inner classes, static inner classes, examples. 46
25 II^ Packages- Defining, creating and accessing a package, 46-
26 II Understanding CLASSPATH, importing packages. 47
Exception handling- Dealing with errors, benefits of exception handling
28 III The classification of exceptions- exception hierarchy, checked exceptions and unchecked exceptions
29 III Usage of try, catch, throw, throws and finally, 50-
30 III^ Rethrowing exceptions, exception specification, 54
31 III^ Built in exceptions, creating own exception sub classes. 54
Multithreading – Differences between multiple processes and multiple threads, thread states
55-
Creating threads, interrupting threads, thread priorities, synchronizing threads
56-
34 III^ Inter-thread communication, producer consumer pattern 59
35 III^ Exploring java.net and java.text. 60
Unit-
OOP Concepts
Object Oriented Programming is a paradigm that provides many concepts such as
inheritance , data binding polymorphism , etc.
Simula is considered as the first object-oriented programming language. The programming
paradigm where everything is represented as an object is known as truly object-oriented
programming language.
Smalltalk is considered as the first truly object-oriented programming language.
OOPs (Object Oriented Programming System)
Object means a real word entity such as pen, chair, table etc. Object-Oriented Programming is
a methodology or paradigm to design a program using classes and objects. It simplifies the
software development and maintenance by providing some concepts:
o Object
o Class
o Inheritance
o Polymorphism
o Abstraction
o Encapsulation
Object
Any entity that has state and behavior is known as an object. For example: chair, pen, table,
keyboard, bike etc. It can be physical and logical.
Class
Collection of objects is called class. It is a logical entity.
When one object acquires all the properties and behaviours of parent object i.e. known as
inheritance. It provides code reusability. It is used to achieve runtime polymorphism.
When one task is performed by different ways i.e. known as polymorphism. For example: to convince the customer differently, to draw something e.g. shape or rectangle etc.
In java, we use method overloading and method overriding to achieve polymorphism.
Another example can be to speak something e.g. cat speaks meaw, dog barks woof etc.
Hiding internal details and showing functionality is known as abstraction. For example: phone call, we don't know the internal processing.
In java, we use abstract class and interface to achieve abstraction.
Binding (or wrapping) code and data together into a single unit is known as encapsulation. For example: capsule, it is wrapped with different medicines.
A java class is the example of encapsulation. Java bean is the fully encapsulated class because all the data members are private here.
application by sharing common code amongst several subclasses. Where equivalent code
exists in two related classes, the hierarchy can usually be refactored to move the common
code up to a mutual superclass. This also tends to result in a better organization of code and
smaller, simpler compilation units.
inherit from a common superclass can be used interchangeably. If the return type of a
method is superclass
Java Programming- History of Java
The history of java starts from Green Team. Java team members (also known
as Green Team ), initiated a revolutionary task to develop a language for digital
devices such as set-top boxes, televisions etc.
For the green team members, it was an advance concept at that time. But, it was
suited for internet programming. Later, Java technology as incorporated by
Netscape.
Currently, Java is used in internet programming, mobile devices, games, e-business
solutions etc. There are given the major points that describes the history of java.
language project in June 1991. The small team of sun engineers called Green
Team.
top boxes.
4) After that, it was called Oak and was developed as a part of the Green project.
There are many java versions that has been released. Current stable release of Java
is Java SE 8.
There is given many features of java. They are also known as java buzzwords. The Java Features given below are simple and easy to understand.
The java comments are statements that are not executed by the compiler and interpreter. The
There are 3 types of comments in java.
The single line comment is used to comment only one line.
Syntax:
The documentation comment is used to create documentation API. To create documentation API, you need
to use javadoc tool.
Syntax:
/**
This
is
documentation
comment
*/
Example:
/** The Calculator class provides methods to get addition and subtraction of given 2 numbers.*/
public class Calculator {
/** The add() method returns addition of given numbers.*/
public static int add( int a, int b){ return a+b;}
/** The sub() method returns subtraction of given numbers.*/
public static int sub( int a, int b){ return a-b;}
}
Compile it by javac tool:
javac Calculator.java
Create Documentation API by javadoc tool:
javadoc Calculator.java
Now, there will be HTML files created for your Calculator class in the current directory. Open the HTML
files and see the explanation of Calculator class provided through documentation comment.
Data types represent the different values to be stored in the variable. In java, there are two types of data types:
o Primitive data types o Non-primitive data types
Data Type Default Value Default size
boolean False 1 bit char '\u0000' 2 byte
byte 0 1 byte
short 0 2 byte
int 0 4 byte long 0L 8 byte
float 0.0f 4 byte
double 0.0d 8 byte
Java Variable Example: Add Two Numbers class Simple{ public static void main(String[] args){ int a= 10 ; int b= 10 ; int c=a+b; System.out.println(c); }}
Output:
The scope of a variable defines the section of the code in which the variable is visible. As a
general rule, variables that are defined within a block are not accessible outside that block.
The lifetime of a variable refers to how long the variable exists before it is destroyed.
Destroying variables refers to deallocating the memory that was allotted to the variables when
declaring it. We have written a few classes till now. You might have observed that not all
variables are the same. The ones declared in the body of a method were different from those
that were declared in the class itself. There are three types of variables: instance variables,
formal parameters or local variables and local variables.
Instance variables
Instance variables are those that are defined within a class itself and not in any method or
constructor of the class. They are known as instance variables because every instance of the
class (object) contains a copy of these variables. The scope of instance variables is determined
by the access specifier that is applied to these variables. We have already seen about it earlier.
The lifetime of these variables is the same as the lifetime of the object to which it belongs.
Object once created do not exist for ever. They are destroyed by the garbage collector of Java
when there are no more reference to that object. We shall see about Java's automatic garbage
collector later on.
Argument variables
These are the variables that are defined in the header oaf constructor or a method. The scope
of these variables is the method or constructor in which they are defined. The lifetime is
limited to the time for which the method keeps executing. Once the method finishes
execution, these variables are destroyed.
Local variables
A local variable is the one that is declared within a method or a constructor (not in the
header). The scope and lifetime are limited to the method itself.
One important distinction between these three types of variables is that access specifiers can
be applied to instance variables only and not to argument or local variables.
In addition to the local variables defined in a method, we also have variables that are defined
in bocks life an if block and an else block. The scope and is the same as that of the block
itself.
Operator in java is a symbol that is used to perform operations. For example: +, -, *, / etc.
There are many types of operators in java which are given below:
o Unary Operator,
o Arithmetic Operator,
o shift Operator, o (^) Relational Operator,
o Bitwise Operator,
o Logical Operator,
o Ternary Operator and
o Assignment Operator.
Output:
WINTER SPRING SUMMER FALL
Enum in java is a data type that contains fixed set of constants.
It can be used for days of the week (SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY and SATURDAY) , directions (NORTH, SOUTH, EAST and WEST)
etc. The java enum constants are static and final implicitly. It is available from JDK 1.5.
Java Enums can be thought of as classes that have fixed set of constants.
Simple example of java enum
class EnumExample1{ public enum Season { WINTER, SPRING, SUMMER, FALL }
public static void main(String[] args) { for (Season s : Season.values()) System.out.println(s); }}
Control Flow Statements
The control flow statements in Java allow you to run or skip blocks of code when special
conditions are met.
The “if” Statement
The “if” statement in Java works exactly like in most programming languages. With the help of
“if” you can choose to execute a specific block of code when a predefined condition is met. The
structure of the “if” statement in Java looks like this:
if (condition) { // execute this code }
The condition is Boolean. Boolean means it may be true or false. For example you may put a
mathematical equation as condition. Look at this full example:
Creating a Stand-Alone Java Application
1. Write a main method that runs your program. You can write this method anywhere. In this example, I'll write my main method in a class called Main that has no other methods. For example: 2. public class Main
Next, find the "My Computer" icon (on your Start menu or desktop), right-click it, and select properties. Click on the Advanced tab, and then click on the Environment variables button. Look at the variables listed for all users, and click on the Path variable. Do not delete the contents of this variable! Instead, edit the contents by moving the cursor to the right end, entering a semicolon (;), and pressing Ctrl-V to paste the path you copied earlier. Then go ahead and save your changes. (If you have any Cmd windows open, you will need to close them.)