You are on page 1of 14

OBJECT ORIENTED PROGRAMMING and

DESIGN with JAVA

ACTIVITY-02

Questions:

1. Study and present

a) type casting in java.

b) what are command line arguments in java?

c) java keywords and their usage.


a) Type casting in java

Type Casting means to change one state to another state and is done by the
programmer using the cast operator. Type Casting is done during the program
design time by the programmer. Typecasting also refers to Narrow
Conversion. Because in many cases, We have to Cast large data type values
into smaller data type values according to the requirement of the operations.
We can also convert large data type values into smaller data type values that’s
why Type Casting and Narrowcasting.

There are two types of type casting:

Widening Type Casting

Converting a lower data type into a higher one is called widening type casting.
It is also known as implicit conversion or casting down. It is done
automatically. It is safe because there is no chance to lose data. It takes place
when:

For ex:
1. public class WideningTypeCastingExample
2. {
3. public static void main(String[] args)
4. {
5. int x = 7; //automatically converts the integer type into long type
6. long y = x; //automatically converts the long type into float type
7. float z = y;
8. System.out.println("Before conversion, int value "+x);
9. System.out.println("After conversion, long value "+y);
10. System.out.println("After conversion, float value "+z);
11. }
12. }

Output
Before conversion, the value is: 7
After conversion, the long value is: 7
After conversion, the float value is: 7.0
Narrowing Type Casting

This requires you to specify the type that you want to convert the value to
using a casting operator. This is necessary when you want to convert a value
of a larger data type to a smaller data type. For example, if you assign a
double value to an integer variable, you need to use explicit type casting to
convert the double value to an integer value. Explicit type casting is required
when you assign a value of a larger data type to a variable of a smaller data
type. This is also known as narrowing conversion. The compiler cannot do this
automatically, so you must use a casting operator to perform the conversion.

For ex:

1. public class NarrowingTypeCastingExample


2. {
3. public static void main(String args[])
4. {
5. double d = 166.66; //converting double data type into long data type
6. long l = (long)d; //converting long data type into int data type
7. int i = (int)l;
8. System.out.println("Before conversion: "+d); //fractional part lost
9. System.out.println("After conversion into long type: "+l); //fractional part
lost
10. System.out.println("After conversion into int type: "+i);
11. }
12. }

Output

Before conversion: 166.66


After conversion into long type: 166
After conversion into int type: 166
b) What are command line arguments in java?

Command Line Arguments

Java command-line argument is an argument i.e. passed at the time of


running the Java program. In the command line, the arguments passed from
the console can be received in the java program and they can be used as
input.
The users can pass the arguments during the execution bypassing the
command-line arguments inside the main() method.
We need to pass the arguments as space-separated values. We can pass
both strings and primitive data types(int, double, float, char, etc) as
command-line arguments.
These arguments convert into a string array and are provided to the main()
function as a string array argument.
When command-line arguments are supplied to JVM, JVM wraps these and
supplies them to args[].
It can be confirmed that they are wrapped up in an args array by checking the
length of args using args.length.
Internally, JVM wraps up these command-line arguments into the args[ ] array
that we pass into the main() function.
We can check these arguments using the args.length method. JVM stores the
first command-line argument at args[0], the second at args[1], the third at
args[2], and so on.

Types of Java Comments:

There are three types of comments in Java.

1.Single Line Comment

2.Multi Line Comment

3.Documentation Comment
Types of Comments:

1) Java Single Line Comment

The single-line comment is used to comment only one line of the code. It is
the widely used and easiest way of commenting on statements.
Single line comments start with two forward slashes (//). Any text in front of //
is not executed by Java.

Syntax:
//This is a single line comment.

class HelloWorld {
public static void main(String[] args) {
int i=10; // i is a variable with value 10
System.out.println(i); //printing the variable i
}
}

OUTPUT: 10

2).Java Multi Line Comment

The multi-line comment is used to comment multiple lines of code. It can be


used to explain a complex code snippet or to comment multiple lines of code
at a time (as it will be difficult to use single-line comments there).
Multi-line comments are placed between /* and */. Any text between /* and */
is not executed by Java.

Syntax:
/*
This
is
multi line
comment
*/
class HelloWorld {
public static void main(String[] args) {
/* Let's declare and
print variable in java. */
int i=10;
System.out.println(i);
/* float j = 5.9;
float k = 4.4;
System.out.println( j + k ); */
}
}

OUTPUT:
10

3) Java Documentation Comment

Documentation comments are usually used to write large programs for a


project or software application as it helps to create documentation API. These
APIs are needed for reference, i.e., which classes, methods, arguments, etc.,
are used in the code.
To create a documentation API, we need to use the javadoc tool. The
documentation comments are placed between /** and */.

Syntax:
/**
*
*We can use various tags to depict the parameter
*or heading or author name
*We can also use HTML tags
*
*/
c) Java keywords and their usage

Java keywords are also known as reserved words. Keywords are particular
words that act as a key to a code. These are predefined words by Java so
they cannot be used as a variable or object name or class name.

A list of Java keywords or reserved words are given below:

S.No Keyword Usage

Specifies that a class or method will be


1. abstract
implemented later, in a subclass

Assert describes a predicate placed in a java


2. assert program to indicate that the developer thinks that
the predicate is always true at that place.

A data type that can hold True and False values


3. boolean
only

4. break A control statement for breaking out of loops.

5. byte A data type that can hold 8-bit data values


S.No Keyword Usage

6. case Used in switch statements to mark blocks of text

7. catch Catches exceptions generated by try statements

A data type that can hold unsigned 16-bit Unicode


8. char
characters

9. class Declares a new class

10. continue Sends control back outside a loop

Specifies the default block of code in a switch


11. default
statement

12. do Starts a do-while loop


S.No Keyword Usage

A data type that can hold 64-bit floating-point


13. double
numbers

14. else Indicates alternative branches in an if statement

A Java keyword is used to declare an enumerated


15. enum
type. Enumerations extend the base class.

Indicates that a class is derived from another


16. extends
class or interface

Indicates that a variable holds a constant value or


17. final
that a method will not be overridden

Indicates a block of code in a try-catch structure


18. finally
that will always be executed
S.No Keyword Usage

A data type that holds a 32-bit floating-point


19. float
number

20. for Used to start a for loop

Tests a true/false expression and branches


21. if
accordingly

22. implements Specifies that a class implements an interface

23. import References other classes

Indicates whether an object is an instance of a


24. instanceof
specific class or implements an interface

25. int A data type that can hold a 32-bit signed integer
S.No Keyword Usage

26. interface Declares an interface

27. long A data type that holds a 64-bit integer

Specifies that a method is implemented with


28. native
native (platform-specific) code

29. new Creates new objects

This indicates that a reference does not refer to


30. null
anything

31. package Declares a Java package

An access specifier indicating that a method or


32. private variable may be accessed only in the class it’s
declared in
S.No Keyword Usage

An access specifier indicating that a method or


variable may only be accessed in the class it’s
33. protected
declared in (or a subclass of the class it’s
declared in or other classes in the same package)

An access specifier used for classes, interfaces,


methods, and variables indicating that an item is
34. public
accessible throughout the application (or where
the class that defines it is accessible)

Sends control and possibly a return value back


35. return
from a called method

36. short A data type that can hold a 16-bit integer

Indicates that a variable or method is a class


37 static method (rather than being limited to one particular
object)
S.No Keyword Usage

A Java keyword is used to restrict the precision


38. strictfp and rounding of floating-point calculations to
ensure portability.

Refers to a class’s base class (used in a method


39. super
or class constructor)

A statement that executes code based on a test


40. switch
value

synchronize Specifies critical sections or methods in


41.
d multithreaded code

Refers to the current object in a method or


42. this
constructor

43. throw Creates an exception


S.No Keyword Usage

Indicates what exceptions may be thrown by a


44. throws
method

Specifies that a variable is not part of an object’s


45. transient
persistent state

Starts a block of code that will be tested for


46. try
exceptions

Specifies that a method does not have a return


47. void
value

This indicates that a variable may change


48. volatile
asynchronously

49. while Starts a while loop

You might also like