Java Tutorial
We are thrilled to introduce you to our comprehensive Java Tutorial, meticulously designed to provide a thorough understanding of Java programming. Java stands as one of the most popular and versatile programming languages in the world, renowned for its robustness, security, and portability. Its widespread use spans various domains, making it an essential skill for aspiring developers and seasoned professionals alike.
- Home
- Java Tutorial
Yes, Java is a Platform Independent language.
Java is one the most famous and most used language in the real world,
3. What are the top Java Features?
3. What are the top Java Features?
Setting up of AWS Account
3. What are the top Java Features?
Overview
Java is a high-level, class-based, object-oriented programming language that is widely used for building applications across various platforms. It was developed by Sun Microsystems in 1995 (later acquired by Oracle) and is designed to have minimal implementation dependencies, making it platform-independent. This means Java code can run on any system that supports the Java Virtual Machine (JVM), making it highly versatile.
1. Is Java Platform Independent if then how?
Yes, Java is a Platform Independent language. Unlike many
programming languages javac compiler compiles the program to form
a bytecode or .class file. This file is independent of the software or
hardware running but needs a JVM(Java Virtual Machine) file
preinstalled in the operating system for further execution of the bytecode.
Although JVM is platform dependent, the bytecode can be created on
any System and can be executed in any other system despite hardware
or software being used which makes Java platform independent.
2. What are the top Java Features?
Java is one the most famous and most used language in the real world,
there are many features in Java that makes it better than any other
language some of them are mentioned below:
3. What are the top Java Features?
4. What are the Memory Allocations available in JavaJava?
A classloader in Java is a subsystem of Java Virtual Machine, dedicated
to loading class files when a program is executed; ClassLoader is the first to load the executable file.
Java has Bootstrap, Extension, and Application classloaders.
5.What are the differences between Heap and Stack Memory in Java?
Stack memory in data structure is the amount of memory allocated to
each individual programme. It is a fixed memory space. Heap memory,
in contrast, is the portion that was not assigned to the Java code but
will be available for use by the Java code when it is required, which is
generally during the programs runtime.
6. What is the default value stored in Local Variables?
Neither the Local Variables nor any primitives and Object references
have any default value stored in them.
7. What are Packages in Java?
Packages in Java can be defined as the grouping of related types
of classes, interfaces, etc providing access to protection and
namespace management.
8.What are the advantages of Packages in Java?
There are various advantages of defining packages in Java
- Packages avoid name clashes
- The Package provides easier access control
- We can also have the hidden classes that are not visible outside and are
- used by the package
- It is easier to locate the related classes.
9. How many types of packages are there in Java?
There are two types of packages in Javã
- User defined packageè
- Build In packages
10.Explain different data types in Java?
There are 2 types of data types in Java as mentioned below
- Primitive Data Typ3
- Non Primitive Data Type or Object Data type
Primitive Data Type: Primitive data are single values with no special
capabilities. There are 8 primitive data types
- boolean: stores value true or false
- byte: stores an 8 bit signed two s complement intege
- char: stores a single 16 bit Unicode character
- short: stores a 16 bit signed two s complement integer
- int: stores a 32 bit signed two s complement integer
- long: stores a 64 bit two s complement integer
- float: stores a single precision 32 bit IEEE 754 floating point
- double: stores a double precision 64 bit IEEE 754 floating point
Non Primitive Data Type: Reference Data types will contain a memory
address of the variable s values because it is not able to directly store
the values in the memory. Types of Non Primitive are mentioned below
- Strings
- Array
- Class
- Object
- Interface
11. Can we declare Pointer in Java?
No, Java doesn t provide the support of Pointer. As Java needed to be
more secure because which feature of the pointer is not provided in
Java
12. What is the default value of byte datatype in Java?
The default value of the byte datatype in Java is 0
13. Differentiate between instance and local variables?
Instance Variable
Declared outside the method, directly invoked by the method. Has a default value.
It can be used throughout the class.
It can be used throughout the class.
Local Variable
Declared within the method.
No default value
The scope is limited to the method
14. Define Copy Constructor in Java?
A Copy Constructor in Java is a constructor that initializes an object
through another object of the same class
15. Define package in Java.
The package is a collective bundle of classes and interfaces and the
necessary libraries and JAR files. The use of packages helps in code
reusability
16. What is the final keyword in Java?
The term final is a predefined word in Java that is used while declaring
values to variables. When a value is declared using the final keyword,
then the variable s value remains constant throughout the programs
execution
17. What are the differences between constructor and method of a class in Java?
Initializing the state of the object is done by constructors. A function
Object () { [native code] }, like methods, contains a group of statements
(or instructions) that are carried out when an object is created. A
method is a group of statements that work together to complete a
certain task and return the outcome to the caller. A method has the
option of working without returning anything
18. What is a static variable?
The static keyword is used to share the same variable or method of a
given class. Static variables are the variables that once declared then a
single copy of the variable is created and shared among all objects at
the class level.
19. Define System.out.println()?
System.out.println() in Java outputs the argument that was supplied to On the monitor, the println() method displays the findings. An
objectname is typically used to call a method.
20. Write a Java Program to print Fibonacci Series using Recursion?
static int n1=0,n2=1,n3=0;
static void printFibonacci(int count){
if(count>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
System.out.print( “+n3);
printFibonacci(count 1);
}
}
int count=10;
System.out.print(n1+” “+n2);//printing 0 and 1
printFibonacci(count 2);//n 2 because 2 numbers are already printed
}
}
21. Can a constructor return a value?
Yes, A constructor can return a value. It replaces the class s current
instance implicitly; you cannot make a constructor return a value
explicitly
22.Explain this keyword in Java?
The term this is a particular keyword designated as a reference keyword. The this keyword is used to refer to the current class properties like method, instance, variable, and constructors.
23. Explain Method Overloading in Java?
The process of creating multiple method signatures using one method name is called Method Overloading in Java. Two ways to achieve
method overloading are:
- Varying the number of argumentè
- Changing the return type of the Method
24. What are operators?
Operators are the special types of symbols used for performing some operations over variables and values.
25. How many types of operators are available in Java?
- Arithmetic Operators
- Assignment Operators
- Logical Operators
- Relational Operators
- Unary Operators
- Bitwise Operators
- Ternary Operators
- Shift Operators.
26. Explain the difference between >> and >>> operators?
Operators like >> and >>> seem to be the same but act a bit differently.
>> operator shifts the sign bits and the >>> operator is used in shifting
out the zero filled bits.
import java.io.*;
// Driver
class GFG {
public static void main(String[] args)
{
int a = 16, b = 1;
// Use of >>
System.out.println(a >> b);
a = 17;
b = 1;
// Use of >>>
System.out.println(a >>> b);
}
}
Output
8
2147483639
27. What is dot operator?
The Dot operator in Java is used to access the instance variables and
methods of class objects. It is also used to access classes and sub
packages from the package.
28. What is an array in Java?
An Array in Java is a data structure that is used to store a fixed size
sequence of elements of the same type. Elements of an array can be
accessed by their index, which starts from 0 and goes up to a length of
minus 1. Array declaration in Java is done with the help of square
brackets and size is also specified during the declaration.
29. What are the types of an array?
There are two types of arrays i.e., Primitive arrays and References Arrays
- Single Dimensional Arrays: Arrays that have only one dimension i.e., an array of integers or an array of strings are known as single dimensional arrays
- Multi Dimensional Arrays: Arrays that have two or more dimensions such as two dimensional or three dimensional arrays.
30. What are the main concepts of OOPs in Java?
The main concepts of OOPs in Java are mentioned below
- Inheritance
- Polymorphism
- Abstraction
- Encapsulation
31. What is an object?
The object is a real life entity that has certain properties and methods associated with it. The object is also defined as the instance of a class. An object can be declared using a new keyword.
32. What are the different ways to create objects in Java?
Methods to create objects in Java are mentioned below
- Using new keyword
- Using new instance
- Using clone() method
- Using deserialization
- Using the newInstance() method of the Constructor class
33. What is the constructor?
Constructor is a special method that is used to initialize objects.
Constructor is called when a object is created. The name of constructor
is same as of the class.
Example: // Class Created class XYZ{
private int val;
// Constructor
XYZ(){
val=0;
} };
34. When can you use the super keyword?
Basically, the super keyword is used to refer to the parent class. When there are the same fields in both parent and child classes, then one can use a super keyword to access data members of the parent class.
35. What do you mean by data encapsulation?
Data encapsulation is one of the properties of OOPS concepts, where all
the data such as variables and methods are enclosed together as a
single unit.
36. How is an infinite loop declared in Java?
An infinite loop can be declared in Java by breaking the logic in the
instruction block.
For example,
for(int i = 1; i > 0; i++)
{
//statements
}
The above code forms an infinite loop in Java.
37. Briefly explain the concept of constructor overloading ?
The concept of constructor overloading refers to having multiple methods in a class with their name being the same as the class name.
The difference lies in the set of parameters passed to the functions.
38. What is a Java Virtual Machine?
It is an abstract machine that provides a runtime environment for Java
programs to run. The JVM interprets the compiled Java code and
executes it on the underlying operating System.
39. Difference between static (class) method and instance method ?
The main difference between a static method and an instance method in Java is that a static method is associated with the class, while an instance method is associated with an instance of the class.
40. Which class is the superclass for all the classes?
The Object class is the only superclass for all classes in Java
41. How many types of constructors are used in Java?
There are two types of constructors in Java as mentioned below
- Default Constructor
- Parameterized Constructor
42. What is an Interface?
An interface in Java is a collection of static final variables and abstract
methods that define the contract or agreement for a set of linked classes. Any class that implements an interface is required to implement a specific set of methods. It specifies the behavior that aclass must exhibit but not the specifics of how it should be implemented.
Syntax:
interface
{
// constant fields
// methds that are abstract by default
}
43. Define Inheritance ?
When an object that belongs to a subclass acquires all the properties
and behavior of a parent object that is from the superclass, it is known
as inheritance. A class within a class is called the subclass and the
latter is referred to as the superclass. Sub class or the child class is said
to be specific whereas the superclass or the parent class is generic.
Inheritance provides code reusability.
44. What is an association?
The association is a relation between two separate classes established through their Objects. It represents Has As relationship.
45. What is Polymorphism?
Polymorphism is defined as the ability to take more than one form It is
of two types namely, Compile time polymorphism or method
overloading a function called during compile time. For instance, take a
class area. Based on the number of parameters it may calculate the
area of a square, triangle, or circle. Run time polymorphism or method
overriding links during run time. The method inside a class overrides
the method of the parent class.
46.What is method overriding?
Method overriding, also known as run time polymorphism is one where
the child class contains the same method as the parent class. For
instance, we have a method named gfg() in the parent class. A method
gfg() is again defined in the sub class. Thus when gfg() is called in the subclass, the method within the class id executed. Here, gfg() within the
class overridden the method outside.
47. What is method overloading?
Method overriding is a method to achieve Run time polymorphism in
Java. Method overriding is a feature that allows a child class to provide
a specific implementation of a method that is already provided by one
of its parent classes. When a method in a child class has the same
name, the same parameters or signature, and the same return type(or
sub type) as a method in its parent class, then the method in the
subclass is said to override the method in the superclass.
48. Why Does Java Array Index Start with 0?
In Java, array indexing starts with 0. This is because an array is a
contiguous block of memory where each element occupies a fixed
amount of space. It is the offset from the start of the array.
Consider the following code:
int[] arr = new int[5];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
In this code, we declare an array of size five and assign values to each
element. The index of the first element is coming 0, and the last element
index is coming 4, which is the size of the array minus 1.
49. Can we overload the main() method?
Yes in Java we can overload the main method to call the main method with the help of its predefined calling method.
50. What is Abstract class?
A class declared as abstract, cannot be instantiated i.e., the object cannot be created. It may or may not contain abstract methods but if a class has at least one abstract method, it must be declared abstract.