Java _Unit-I_part1

Next Topic Inheritance-Method Overriding-Abstract Classes Verses Interfaces

1. Explain the features of Java?

Ans:

Java has many advanced features when compared to other programming languages. 

Compiled and Interpreted: First, Java compiler translates source code into bytecode instructions. Bytecodes are not machine instructions and therefore, in the second stage, Java interpreter generates machine code that can be directly executed by the machine that is running the Java program. We can thus say that Java is both a compiled and an interpreted language.




Platform-Independent and Portable: Java programs can be easily moved from one computer system to another, anywhere and anytime. Changes and upgrades in operating systems, processors and system resources will not force any changes in Java programs. This is the reason why Java has become a popular language for programming on Internet.


Object-Oriented: Java is a true object-oriented language. Almost everything in Java is an object. All program code and data reside within objects and classes. Java comes with an extensive set of classes, arranged in packages, that we can use in our programs by inheritance. The object model in Java is simple and easy to extend.


Robust and Secure: Java is a robust language. It provides many safeguards to ensure reliable code. It has strict compile time and run time checking for data types and errors. It provides automatic garbage-collection. Java also allows exception handling to catch runtime errors. Java systems not only verify all memory access but also ensure that no viruses are communicated with an applet. The absence of pointers in Java ensures that programs cannot gain access to memory locations without proper authorization.


Distributed: Java is designed as a distributed language for creating applications on networks. It has the ability to share both data and programs. Java applications can open and access remote objects on Internet. This enables multiple programmers at remote locations to collaborate and work together on a single project. 


Simple, Small and Familiar: Java is a small and simple language. Familiarity is another important feature of Java. To make the language look familiar to the existing programmers, it was modeled on C and C++ languages. Java uses many constructs of C and C++ and therefore, Java code "looks like a C++" code. In fact, Java is a simplified version of C++. Multithreaded and Interactive: Multithreaded means handling multiple tasks simultaneously.


Java supports multithreaded programs; This means that we need not wait for the application to finish one task before beginning another. For example, we can listen to an audio clip while scrolling a page and at the same time download an applet from a distant computer. 


High Performance: Java architecture is designed to reduce overheads during runtime. Further, the incorporation of multithreading enhances the overall execution speed of Java programs.


Dynamic and Extensible: Java is a dynamic language. Java is capable of dynamically linking in new class libraries, methods, and objects. Java programs support functions written in other languages such as C and C++. These functions are known as native methods. This facility enables the programmers to use the efficient functions available in these languages.


2.What are the  steps involved in developing a Java Program?

Ans:

Implementation of a Java application program involves a series of steps. They include: 

1. Creating the program 

2. Compiling the program 

3. Running the program 



Creating the Program: We can create a program using any text editor like notepad. But, we need to make sure that the program name should be saved with the extension “.java”. Advisably, the name of the program should match with the name of the public class or a class that has main() method.


Example: assumed that the program name is “Test.java”

class Test

{

public static void main(String as[])

{

System.out.println(“Java Program”);

}



Compiling the program: Java program involves two steps for its execution: one compilation and the other execution. We use “javac” tool to compile java program. This yields byte code file whose extension is “.class”. This is an intermediate file and not directly executable.

Syntax: javac Test.java 


Running the Program: Now, java program is interpreted by the tool “java” which as an interpreter. This is different for different machines. This tool generates executable code for the machine in use. Thus one can view the output of the java program.


Syntax: java Test The complete steps to run a java program are listed below:

1. Type the program in the DOS editor or notepad. Save the file with a .java extension.

2. The file name should be the same (advisably) as the class, which has the main method.

3. To compile the program, using javac compiler, type the following on the command line: Example: javac Test.java

4. After compilation, run the program using the Java interpreter.




Example: java Test

5. The program output will be displayed on the command line.




3.What are the different data types in Java?

Ans:

Data-type: 

Data-type specifies what type of value a variable can store. It also specifies its size. Every variable in Java has a data-type. Java language is rich in its data types. The categories of different data-types are shown below. The two main types are Primitive and non-primitive types.



1. Integer type: An integer type of variables can hold integer constants. Java supports four types of integer data types. They are byte, short, int and long. The following table shows size in bytes and range of values each data type can represent. Integer can be converted to long type by typing ‘L’ after them.




2. Floating point type: This data type is used to represent numbers containing fractional values. They are two types of data-types under this category, namely, float and double. float type number are single precision numbers while double type number are double precision numbers. By default float are double precision and can converted to single precision by adding ‘f’ at the end. Double precision types are used when greater precision is required in storage of floating point. Floating point data types have special NaN (Not a Number) value for undefined numbers like 0/0 etc.




3. Character type: Java provides a data type called char to hold character values. It is of size 2 bytes and assumes single character. The character value must be enclosed in single quotes.



4. Boolean type: It can have only two values, either ‘true’ or ‘false’. Its keyword is ‘boolean’ and use only 1 bit. All comparison operators return boolean type values. The words ‘true’ and ‘false’ cannot be used as identifiers.





4.Explain Type Casting.
Ans:

Type casting is a process to convert one data type to another. It makes the variable compatible temporarily. We often encounter situations where there is a need to store a value of one type into a variable of another type. In such situations, we must cast the value to be stored by preceding it with the type name in parentheses. Casting is often necessary when a method returns a type different than the one we require.



Four integer types can be cast to any other type except boolean. Casting into a smaller type may result in a loss of data. Similarly, the float and double can be cast to any other type except boolean. Again, casting to smaller type can result in a loss of data. Casting a floating point value to an integer will result in a loss of the fractional part. If the following order is used for type casting, it guarantees in no loss of information.



byte -> char -> short -> int -> long -> float -> double
for example
• byte value can be converted to int or double
• int value can be converted to long or float
The syntax for casting is: type variable1 = (type) variable2;

Type casting can be of two types:

Implicit type Casting (Automatic Conversion): when constants and variables of different types are mixed in an expression they are converted to the same type. This conversion is done implicitly by the C compiler. The C compiler converts all the operands to the type of the largest operand. For example if an expression involves int and float, int type gets converted to float type.
Example:
float x=32;
The value of the x will be ’32.0’, because of implicit conversion of value from int to float


Explicit type Casting: if a user forcefully changes the data type into other allowable data type it is said to be explicit type casting.


Example:
int to float:
float x;
x=5/2 /* value of x will be 2.0 */
x=(float)5/2; /* value of x will be 2.5 */
float to int:
int x;

x= 3.2 /* causes error */
x=(int)3.2 /* x will be 3 */



5. Explain different control statements
Ans:
Conditional control structure (Branching):
The if statement is implemented in four forms:
1. Simple if statement
2. if…else statement
3. Nested if…else statement
4. else if ladder



a) if: If structure is also called as conditional statement. In If, statements get executed only when the condition is true. It omits the condition based statements when the condition is false. Braces are not necessary if only one statement is related to the condition.

if (condition)
 {
statements
}



b) if else: Statements in if block get executed only when the condition is true and statements in else block get executed only when the condition is false.
if (condition) {
Statements
}
else {
Statements
}



(c). if-else if:
Statements get executed in loops only when the corresponding conditions are true. Statements in the final else block get executed when all other conditions
are false. The control checks a condition only when all the afore-mentioned conditions are false.


if ( x > 0 )
System.out.println( “positive”);
else if ( x < 0)
System.out.println(“negative ”);
else
System.out.println( “zero”);


(d) nested if: Writing if in another if is nested-if. Inner if is processed only when outer if’s condition is true. Hence, statements in inner-if get executed only when condition1 and condition2 are true.



(e)switch statement: The complexity of a program increases when the number of alternatives increases in if statement. The program becomes difficult to read and follow. It may confuse even the designer.
Java has a built-in multi way decision statement known as switch. The switch statement tests the value of a given variable against a list of case values and when a match is found, a block of statements associated with that case is executed.



The general form of the switch statement is as below:
switch(expression)
{
case value1: stat1;
break;
case value2: stat2;
break;
.
.
.
.
.
case valuen: statn;
break;
default: defaultstat;
}



The expression is an integer or character expression. value1,value2,…valuen are know as case labels. There is no need to put braces around these blocks but it is important to note that case labels end with a colon.
When the switch statement is executed, the value of the expression is successively compared against the values value1,value2,…valuen. If a case is found whose value matches with the value of the expression, then the block of the statements that follow the case are executed. The break statement at the end of each block signals the end of a particular case and causes an exit from the switch statement. The default is an optional case. When preset, it will be executed if the value of the expression does not match with any of the case values.


Visit https://degreecsa.blogspot.com


No comments:

Post a Comment