Java Programming Basics and Concepts
Java Programming Basics and Concepts
JAVA NOTES
Debugger
Form designer etc.
Java is one of most popular high level 'Object Oriented' programming It helps in writing code, compiling and executing java programs easily.
language and has been used widely to create various computer applications. What makes Java programs platform independent and highly
What are OOPs? portable?
Answer – OOPs stands for Object Oriented Programming, Java is an Object Java compiler translates Java Code into Java Bytecode (a highly optimized
Oriented Programming language. In an Object Oriented Programming set of instructions). When the bytecode (also called a Java class file) is to be
language, a program is a collection of objects that interact with other objects run on a computer, a Java interpreter, called the Java Virtual Machine
to solve a problem. Each object is an instance of a class. (JVM), translates the bytecode into machine code and then executes it. The
advantage of such an approach is that once a programmer has compiled a
Applications of java Java program into bytecode, it can be run on any platform (say Windows,
Database applications Linux, or Mac) as long as it has a JVM running on it. This makes Java
Desktop applications programs platform independent and highly portable.
Mobile applications
To write a Java program, we need:
Web based applications
✓ Text Editor (for writing the code),
Games etc.
✓ Java compiler, (for compiling the code into bytecode)
What is JVM? Java Integrated Development Environments (IDES) like NetBeans IDE,
JVM stands for Java Virtual Machine which is basically a java interpreter include text editor as well as Java Compiler.
which translates the byte code into machine code and then executes it. What is NetBeans?
Java NetBeans IDE is open source IDE used for writing java code,
The advantage of JVM is that once a java program is compiled into byte
compiling and executing java programs conveniently.
code. It can be run on any platform.
What is Variable?
What is byte code?
A variable is a storage location for information whose value may vary while
• Byte code is a highly optimized intermediate code produced when a java
a programme is running. A variable is, technically speaking, the name of a
program is compiled by java compiler.
storage area in the computer’s internal memory. The data present there
• Byte code is also called java class file which is run by java interpreter
serves as the variable’s value.
called JVM.
What is Java IDE?
Java Integrated Development Environment is software development kit
equipped with int
4. It is good practice to make variable names meaningful. The name char Character 16-bit
should indicate the use of that variable.
boolean True or False 1-bit
5. You can define multiple variables of the same type in one statement
by separating each with a comma. String Variable
String variables, also known as alphanumeric or character variables, have
What is Datatype?
values that are interpreted as text. In other words, string variables’ values
When declaring variables, we have to specify the data type of information could be made up of letters, numbers, or symbols.
that the member will hold – integer, fractional, alphanumeric, and so on.
Example- String first_name = “Mayank”;
The type of a variable tells the compiler, how much memory to reserve
String last_name = “Saxena”;
when storing a variable of that type.
NOTE: A variable of the primitive data type char can be used to store a
Java Datatype is divided into two types –
single character. To assign a value to a char variable we enclose the
1. Primitive data types – includes byte, short, int, long, float, double, character between single quotes.
boolean and char. char middle_name = 'M';
2. Non-primitive data types – such as String, Arrays and Classes.
What is the difference between local and global variables?
3. Primitive data types
Answer – Depending on their scope, variables are divided into global
A reserved term is used to identify a primitive type, which is predefined by variables and local variables. Local variables can only be accessed within
the language. The Java programming language’s support eight primitive the function or block in which they are defined, In other hand the global
data types. variables, which can be used worldwide throughout the entire programme.
What is Operator and what are the different types of Operator? c. Assignment Operator
Answer – Operators are special symbols in a programming language and
perform certain specific operations. Java support –
a. Arithmetic Operators : +, -, *, /, %, ++, —
b. Relational Operators : ==, !=, >, <, >=, <=
c. Assignment Operators : =, +=, -=, *=, /=, %=
d. Logical Operators : &&, ||, !
a. Arithmetic Operators
if-else statements can also be nested – you can have another if-else
statement within an outer if or else statement. The example below is a
nested if.
Repetition Structures
Looping is used to execute a single statement or a block of statement n
times till the condition is true. In simple words we can say that loop is a
way to keep executing either a single statement or a block of statement n Example:
times till the condition is true i.e. until a given condition becomes false. Program Initialization Condition Increment/Decrement
Print 1-10 i=1 i<=10 i=i+1
Print 2-20 i=2 i<=20 i=i+2
Print 10-1 i=10 i>=1 i=i-1
Print 20-2 i=20 i>=2 i=i-2
Print table 5 i=5 i<=50 i=i+5
Example :
The ability of a computer to perform the same set of actions again and again
is called looping.
The sequence of statements that is repeated again and again is called the
body of the loop.
The test conditions that determine whether a loop is entered or exited is
constructed using relational and logical operators.
A single pass through the loop is called an iteration.
For example, a loop that repeats the execution of the body three times goes b) The Do While loop- The do while statement evaluates the test after
through three iterations. Java provides three statements – executing the body of a loop. The structure of the Java do while statement is
1. while as shown –
2. do while
3. for
a) While loop - The while statement evaluates the test before executing the
body of a loop. The structure of the Java while statement is as shown: Example :
This loop is an infinite loop - The semicolon after the while causes the
statement to be repeated as the null statement (which does nothing). If
the semi colon at the end is removed the loop will work as expected.
Common Coding Errors: We can initialize the array statically (that is at compile time) as shown
1) The for Loop Initial value is greater than the limit value and the below:
loop increment is positive. In this case, body of the loop will never double[]marks = {346, 144, 103, 256.5, 387.5};
be executed. To print all the marks, we can use a for loop, varying the loop index
2) Initial value is lesser than the limit value and the loop increment is from 0 to 4.
negative. In this case also, body of the loop will never be executed. for (int i = 0; i< 5; i++)
3) Placing a semicolon at the end of a for statement. [Link](marks[i]);
4) Executing the loop either more or less times than the desired User Defined Methods
number of times. A method in Java is a block of statements grouped together to perform a
5) Using a loop index (declared within the loop) outside the loop. specific task. A method has a name, a return type, an optional list of
parameters, and a body. The structure of a Java method is as below:
A special return type void can be used if a method does not return any a) Constructors
value. For example, a method that just prints a string need not have a return A special method member called the constructor method is used to initialize
value can use void as the return type. the data members of the class (or any other initialization is to be done at
void print_message(String message) { time of object creation).
[Link]("The message is: "+ message); Tips : The constructor has the same name as the class, has no return
} type, and may or may not have a parameter list.
This method can be called with a statement such as Whenever a new object of a class is created, the constructor of the class is
print_message("This is a secret message!"); invoked automatically. We do not call the constructor explicitly.
The optional finally block is always executed when the try block exits. This
means the finally block is executed whether an exception occurs or not.
Assertion
An assertion is a useful mechanism for effectively identifying/detecting and
correcting logical errors in a program. An assert statement states a condition
that should be true at a particular point during the execution of the program.
There are two ways to write an assertion:
1) assert expression; 1) We can convert a string into its integer value. The following statement
2) assert expression1 : expression2 converts the string “3456” into the integer3456 and stores it in the int
variable d.
Threads int d = [Link](“3456”);
Thread refers to the smallest unit of execution within a process, in turn is an 2) toString() method allows conversion from an integer value to a String.
independent program that runs in its own memory space. A multithreaded String s = [Link](3456);
programme can execute numerous tasks simultaneously for the best possible
resource utilisation on the computer. A multithreaded program consists of
two or more parts called threads each of which can execute a different task PYQs
independently at the same time. 1) State TRUE or FALSE:
In Java, threads can be created in two ways: Private data members of a class cannot be accessed outside the class
1. By extending the Thread class however, you can give controlled access to data members outside the
2. By implementing the Runnable interface class through getter and setter methods.
2) Differentiate between the following two java statements:
Wrapper Classes char Alpha= ‘A’ ; //Statement #1
Wrapper classes are a set of classes that provide an object representation for String Name= “Manish”; //Statement #2
primitive data types. By default, the primitive datatypes (such as int, float, 3) Convert the following Java code from for loop to while loop without
and so on) of Java are passed by value and not by reference. Primitive changing the logic.
datatypes may occasionally need to be passed by reference. When that int X=4, Y=3;
happens, you can use the Java wrapper classes. These classes encapsulate int Pow=1;
the primitive datatype in an object. For instance, an int variable is held by for (int i=1;i<=Y;i++)
the Integer wrapper class. Pow*=X;
Consider the following two declarations: [Link](Pow);
int a = 50; // using primitive datatype 4) Give the output for the following code:
Integer b = new Integer(50); // using wrapper class String myString = "Welcome to Java";
a) [Link] ([Link]("Session"));
b) [Link] ([Link]());
c) [Link] ([Link]("to","2"));
5) Write Java code to do the following:
i) Create an array Marks that stores values 78,65,85,91,82
ii) To print all the values of the array Marks using a loop.
iii) Display the average stored in the array Marks.
6) Exception thrown by any statement in try block in a Java program is
handled in _____________ block.
7) Which access modifier in Java is used to make a data member or a
method member of a class visible only within the class?
Key Points
8) In Java, _____________ statement is used to execute a block of code 22) How can you write multiline comments in java program?
matching one value out of many possible values. 23) Sagar has declared an array whose last element is having an
9) A/An _____________ in Java is a block of statements grouped together index/position as 10. What would be the size of this array?
to perform a specific task. 24) Write the output of following code:
10) What are the different ways to create threads in Java programming? class production {
11) With reference to Java programming language, define assertion. State int inp = 50;
any one way to write an assertion. void change(int inp) {
12) Consider the following string variables : [Link]("Change"+[Link]);
String str1 = "Hello"; inp=inp+100;
String str2 = "Java Programming"; }
Write the code statements for the following in Java : public static void main(String args[]) {
a) To display the string str1 into upper case. production po=new production();
b) To display the character at the index 6 in string str2. [Link]("before
c) To display the index of the first occurrence of letter ‘l’ in the string change"+[Link]); [Link](500);
str1. [Link]("after
d) To display the string str1 after replacing letter ‘l’ with '*'. change"+[Link]);
13) Differentiate between Entry controlled loop and Exit controlled loop. }
14) ________ method of Arrays class in Java helps us to search for a }
specific element in the array. 25) Consider the following class :
15) Predict the output : public class Stud {
[Link]("Hello"); String Rno;
[Link]("Java"); String Sname;
16) ________ keyword is used to create a new class in Java program. String Address;
17) Explain the access modifiers and their types in Java. double marks;
18) What is the difference between = and == operator in Java ? Give
void display( ){
example to support your answer.
[Link]("Rno : " +Rno);
19) Consider the following code:
int x = 1; [Link]("Student name : " +Sname);
while(x<=5); [Link]("Address : " +Address);
x = x +1; [Link]("Marks : " +marks);
(a) Name the coding error shown in the above code. }
(b) What is the reason of the error? }
(c)Write correct java code. Write a command to create a constructor of the above class that
20) What is the need of a constructor in OOP? Explain all features of a initializes the data members of a class.
constructor.
21) Write a code in java using a for loop to display all odd numbers between
1 to 10. Is for loop an entry controlled loop or exit control loop?