JAVA - Long Answer
JAVA - Long Answer
The arguments passed from the console can be received in the java program and it can be
used as an input.
So, it provides a convenient way to check the behavior of the program for the different values.
You can pass N (1,2,3 and so on) numbers of arguments from the command prompt.
for(int i=0;i<args.length;i++)
System.out.println(args[i]);
4.What is type casting? Differentiate between implicit and explicit type casting with
examples.
Type casting
Convert a value from one data type to another data type is known as type casting.
int x = 7;
long y = x;
float z = y;
Output
In the above example, we have taken a variable x and converted it into a long type. After that,
the long type is converted into the float type.
double -> float -> long -> int -> char -> short -> byte
Advertisement
In the following example, we have performed the narrowing type casting two times. First, we
have converted the double type into long data type after that long data type is converted into int
type.
NarrowingTypeCastingExample.java
double d = 166.66;
long l = (long)d;
int i = (int)l;
Output
Literals in Java
In Java, literal is a notation that represents a fixed value in the source code. In lexical analysis,
literals of a given type are generally known as tokens. In this section, we will discuss the
term literals in Java.
Literals
In Java, literals are the constant values that appear directly in the program. It can be assigned
directly to a variable. Java has various types of literals. The following figure represents a literal.
Types of Literals in Java
There are the majorly four types of literals in Java:
1. Integer Literal
2. Character Literal
3. Boolean Literal
4. String Literal
Integer Literals
Integer literals are sequences of digits. There are three types of integer literals:
Decimal Integer: These are the set of numbers that consist of digits from 0 to 9. It may have a positive
(+) or negative (-) Note that between numbers commas and non-digit characters are not permitted. For
example, 5678, +657, -89, etc.
Octal Integer: It is a combination of number have digits from 0 to 7 with a leading 0. For example, 045,
026,
Binary Integer: Base 2, whose digits consists of the numbers 0 and 1 (you can create binary literals in Java
SE 7 and later). Prefix 0b represents the Binary system. For example, 0b11010.
Real Literals
The numbers that contain fractional parts are known as real literals. We can also represent real
literals in exponent form. For example, 879.90, 99E-3, etc.
Backslash Literals
Java supports some special backslash character literals known as backslash literals. They are
used in formatted output. For example:
\n: It is used for a new line
Character Literals
A character literal is expressed as a character or an escape sequence, enclosed in
a single quote ('') mark. It is always a type of char. For example, 'a', '%', '\u000d', etc.
String Literals
String literal is a sequence of characters that is enclosed between double quotes ("") marks. It
may be alphabet, numbers, special characters, blank space, etc. For example, "Jack",
"12345", "\n", etc.
Advertisement
o Floating-point literals for float type end with F or f. For example, 6f, 8.354F, etc. It is a 32-bit
float literal.
o Floating-point literals for double type end with D or d. It is optional to write D or d. For
example, 6d, 8.354D, etc. It is a 64-bit double literal.
o It can also be represented in the form of the exponent.
Floating:
Decimal:
Boolean Literals
Boolean literals are the value that is either true or false. It may also have values 0 and 1. For
example, true, 0, etc.
Class Literals
Advertisement
Class literal formed by taking a type name and appending .class extension. For
example, Scanner.class. It refers to the object (of type Class) that represents the type itself.
Invalid Literals
There is some invalid declaration of literals.
float g = 6_.674f;
float g = 6._674F;
int x = 77_;
int y = 0_x76;
int z = 0X_12;
int z = 0X12_;
Advertisement
LiteralsExample.java
{
public static void main(String args[])
System.out.println(count);
System.out.println(floatVal);
System.out.println(cost);
System.out.println(hexaVal);
System.out.println(binary);
System.out.println(alpha);
System.out.println(str);
System.out.println(boolVal);
System.out.println(octalVal);
System.out.println(stuName);
System.out.println(ch1);
System.out.println(ch2);
Output:
987
4534.99
19765.567
2020
26
p
Java
true
55
null
!
backslash literal
?
UNIT -II
In Java, a constructor is said to be overloaded when a class has more than one constructor, each
differing in the number or type of parameters. Overloaded constructors allow the creation of objects
in different ways, providing flexibility based on the needs of the program. Each constructor performs
a specific initialization based on the passed arguments.
Example:
class Student
{
String name;
int age;
String department;
// Constructor 1: No parameters
public Student()
{
name = "Unknown";
age = 0;
department = "Not assigned";
}
Output:
Name: Unknown
Age: 0
Department: Not assigned
Name: Alice
Age: 0
Department: Not assigned
Name: Bob
Age: 20
Department: Not assigned
Name: Charlie
Age: 22
Department: Computer Science
Explanation:
9.What is method overriding in Java? How does it differ from method overloading?
If subclass (child class) has the same method as declared in the parent class, it is known
as method overriding in Java.
In other words, If a subclass provides the specific implementation of the method that has been
declared by one of its parent class, it is known as method overriding.
//class object.
class Vehicle{
obj.run();
}
Output:
Vehicle is running
Problem is that I have to provide a specific implementation of run() method in subclass that is
why we use method overriding.
class Vehicle{
//defining a method
obj.run();//calling method
Output:
class Bank{
class Test2{
Output:
SBI Rate of Interest: 8
ICICI Rate of Interest: 7
AXIS Rate of Interest: 9
2. What is the this keyword in Java? Explain its significance with examples.
class Student{
int rollno;
String name;
float fee;
rollno=rollno;
name=name;
fee=fee;
class TestThis1{
s1.display();
s2.display();
}}
Output:
0 null 0.0
0 null 0.0
In the above example, parameters (formal arguments) and instance variables are same. So, we
are using this keyword to distinguish local variable and instance variable.
int rollno;
String name;
float fee;
this.rollno=rollno;
this.name=name;
this.fee=fee;
void display()
class TestThis2
s1.display();
s2.display();
Output:
If local variables(formal arguments) and instance variables are different, there is no need to use
this keyword like in the following program:
int rollno;
String name;
float fee;
rollno=r;
name=n;
fee=f;
class TestThis3
s1.display();
s2.display();
Output:
void n()
System.out.println("hello n");
//m();//same as this.m()
this.m();
class TestThis4{
A a=new A();
a.n();
}}
Output:
hello n
hello m
A(){System.out.println("hello a");}
A(int x){
this();
System.out.println(x);
class TestThis5{
A a=new A(10);
}}
Output:
hello a
10
class A{
A(){
this(5);
System.out.println("hello a");
A(int x){
System.out.println(x);
class TestThis6{
A a=new A();
}}
Output:
5
hello a
class Student{
int rollno;
String name,course;
float fee;
this.rollno=rollno;
this.name=name;
this.course=course;
this(rollno,name,course);//reusing constructor
this.fee=fee;
class TestThis7{
s1.display();
s2.display();
}}
Output:
class Student{
int rollno;
String name,course;
float fee;
this.rollno=rollno;
this.name=name;
this.course=course;
this.fee=fee;
this(rollno,name,course);//C.T.Error
class TestThis8{
s1.display();
s2.display();
}}
Output:
class S2{
System.out.println("method is invoked");
void p(){
m(this);
S2 s1 = new S2();
s1.p();
Output:
method is invoked
class B{
A4 obj;
B(A4 obj){
this.obj=obj;
void display(){
}
class A4
int data=10;
A4(){
B b=new B(this);
b.display();
A4 a=new A4();
Output:10
return this;
A getA(){
return this;
}
class Test1{
new A().getA().msg();
Output:
Hello java
class A5{
void m(){
A5 obj=new A5();
obj.m();
Output:
A5@22b3ea59
A5@22b3ea59