Professional Documents
Culture Documents
CORE JAVA
ASSIGNMENT 1
Q.1 Explain any five features of Java language.
Ans: The main features of Java language:
Simple: Java is one of the simplest programming languages to learn and master in order
to grasp the concept of Object-oriented learning.
Object-Oriented Programming Language: This is one of the primary reasons why Java
is so popular amongst developers. Java strictly conforms to the rules of Object Oriented
Programming or OOP. Object-oriented development includes concepts of Objects and
Classes and many more. This enables developers to have a wide variety of options for
designing their software.
High Performance: Though java is not as fast as compiled languages like C, C++ but
java still has some of the good features like just-in-time compiler, multithreading,
garbage collection etc. that increases the performance of java near to compiled languages.
Platform Independent: Java is a platform independent language. In java you can write
and compile your program on one machine (eg. windows) and then you can run the
compiled code(.class file) on any other machine (eg. linux or macos). That is what the
platform independent means in java, program written and compiled on one machine can
be run on any other machine.
NAME: VANSH DILIP NAGDA ROLL NO: 381 CLASS: SYIT
Ans: Code:
Ans: Platform independent means the execution of the program is not restricted by the type of
operating system installed and hence we can execute the java program at any type of
environement available.
How:
Java is a platform independent language because of the magic of java which is bytecode.
In java, when we execute the source code,it generates the .class file which contains the
bytecodes. Bytecodes are interpreted by JVM which is available with every type of OS
we install.
On the other side the source code written in C or C++ gets converted into an object code
which is machine and OS dependent and hence are termed as Platform dependent.
Ans: A typical Structure of a Java program would contain the following elements:
Package declaration: Classes in java could be placed in different directories/packages
based on the module they are used in or the functionality it provides. For all classes that
belong to a single parent source directory, path from source directory is considered as
package declaration.
Class definition: A name should be given to class in java file. This name is used while
creating an object of the class, in other classes/programs.
Class variables: Variables are the means of storing the values of parameters that are
required during execution of the program. Variables declared with modifiers have
different scopes, which define the life of a variable. We shall see in detail about the
modifiers like global, local, static and private at Variables in Java.
NAME: VANSH DILIP NAGDA ROLL NO: 381 CLASS: SYIT
Code :
class A{
public static void main(String args[]){
for(int i=0;i<args.length;i++)
System.out.println(args[i]);
}
}
compile by > java A.java
run by > java A Vansh Nagda 8 1 abc
Output: Vansh
Nagda
8
1
abc
Q.7 Explain various data types available in java.
Ans: Data types specify the different sizes and values that can be stored in the variable. There
are two types of data types in Java:
NAME: VANSH DILIP NAGDA ROLL NO: 381 CLASS: SYIT
1. Primitive data types: The primitive data types include boolean, char, byte, short, int,
long, float and double.
2. Non-primitive data types: The non-primitive data types include Classes, Interfaces,
and Arrays.
Ans: The Java if-else statement also tests the condition. It executes the if block if condition is
true otherwise else block is executed. If the conditional expression returns true, then a particular
statement(s) will execute otherwise other statement(s) will execute.
Example:
Code:
public class IfElseExample {
public static void main(String[] args) {
//defining a variable
int number=13;
//Check if the number is divisible by 2 or not
if(number%2==0){
System.out.println("even number");
}else{
System.out.println("odd number");
}
}
}
Ans: The main difference between for loop, while loop, and do while loop is:
While loop checks for the condition first. so it may not even enter into the loop, if the
condition is false.
do while loop, execute the statements in the loop first before checks for the condition. At
least one iteration takes places, even if the condition is false.
for loop is similar to while loop except that initialization statement, usually the counter
variable initialization a statement that will be executed after each and every iteration in the
loop, usually counter variable increment or decrement
NAME: VANSH DILIP NAGDA ROLL NO: 381 CLASS: SYIT
Example:
Here we have created an object obj of class Hello and then we displayed the instance
variable name of the object.
As you can see that the output is HelloWorld which is what we have passed to the
name during initialization in constructor
. This shows that when we created the object obj the constructor got invoked. In this
example we have used this keyword, which refers to the current object, object obj in this
example.
Code:
public class Hello {
String name;
//Constructor
Hello(){
this.name = "HelloWorld";
}
public static void main(String[] args) {
Hello obj = new Hello();
System.out.println(obj.name);
}
}
Output: HelloWorld