JAVA

Explain features of JAVA. 

Features of java is discussed below:
Simple Java was designed to be easy for the professional programmer to learn and use effectively. Java is easy to master. If we have Basic concepts of object-oriented programming, learning Java will be even easier. Best of all, if you are an experienced C++ programmer, moving to Java will require very little effort. Because Java inherits the C/C++ syntax and many of the object oriented features of C++, most programmers have little trouble learning Java.
Object-Oriented  Java was not designed to be source-code compatible with any other language. This allowed the Java team the freedom to design with a blank slate. One outcome of this was a clean, usable, pragmatic approach to objects.
Robust : The multiplatformed environment of the Web places extraordinary demands on a program, because the program must execute reliably in a variety of systems. Thus, the ability to create robust programs was given a high priority in the design of Java. To gain reliability, Java restricts you in a few key areas to force you to find your mistakes early in program development. At the same time, Java frees us from having to worry about many of the most common causes of programming errors. o Because Java is a strictly typed language, it checks your code at compile time. However, it also checks your code at run time. Many hard-to-track-down bugs that often turn up in hard-to reproduce run-time situations are simply impossible to create in Java. Knowing that what you have written will behave in a predictable way under diverse conditions is a key feature of Java. o To better understand how Java is robust, consider two of the main reasons for program failure: memory management mistakes and mishandled exceptional conditions (that is, runtime errors). Memory management can be a difficult, tedious task in traditional programming environments.
Multithreaded Java was designed to meet the real-world requirement of creating interactive, networked programs. To accomplish this, Java supports multithreaded programming, which allows you to write programs that do many things simultaneously. The Java run-time system comes with an elegant yet sophisticated solution for multiprocess synchronization that enables you to construct smoothly running interactive systems. Java’s easy-to-use approach to multithreading allows you to think about the specific behavior of your program, not the multitasking subsystem.
Architecture-Neutral Acentral issue for the Java designers was that of code longevity and portability. One of the main problems facing programmers is that no guarantee exists that if you write a program today, it will run tomorrow—even on the same machine. Operating system upgrades, processor upgrades, and changes in core system resources can all combine to make a program malfunction. The Java designers made several hard decisions in the Java language and the Java Virtual Machine in an attempt to alter this situation.
Interpreted and High Performance Java enables the creation of cross-platform programs by compiling into an intermediate representation called Java bytecode. This code can be executed on any system that implements the Java Virtual Machine. Most previous attempts at cross-platform solutions have done so at the expense of performance. As explained earlier, the Java bytecode was carefully designed so that it would be easy to translate directly into native machine code for very high performance by using a just-in-time compiler. Java run-time systems that provide this feature lose none of the benefits of the platform-independent code.
Distributed Java is designed for the distributed environment of the Internet because it handles TCP/IP protocols. In fact, accessing a resource using a URL is not much different from accessing a file. Java also supports Remote Method Invocation (RMI). This feature enables a program to invoke methods across a network. 
Dynamic Java programs carry with them substantial amounts of run-time type information that is used to verify and resolve accesses to objects at run time. This makes it possible to dynamically link code in a safe and expedient manner. This is crucial to the robustness of the Java environment, in which small fragments of bytecode may be dynamically updated on a running system.

Explain primitive Data types of JAVA. 

Java defines 8 primitive types:
byte : Smallest integer type o It is a signed 8-bit type (1 Byte)
Range  is -128 to 127
Especially useful when working with stream of data from a network or file
Example:  byte b = 10;
short  : short is signed 16-bit (2 Byte) type
Range : -32768 to 32767
It is probably least used Java type
Example: short vId = 1234;
int : The most commonly used type
It is signed 32-bit (4 Byte) type
Range: -2,147,483,648 to 2,147,483,647
Example: int a = 1234;
long : long is signed 64-bit (8 Byte) type
It is useful when int type is not large enough to hold the desired value
Example: long soconds = 1234124231;
char : It is 16-bit (2 Byte) type o Range: 0 to 65,536
Example: char first = ‘A’;  char second = 65;
float : It is 32-bit (4-Byte) type
It specifies a single-precision value o Example:  float price = 1234.45213f
double : It uses 64-bit (8-Byte)
All math functions such as sin(),cos(),sqrt() etc… returns double value
Example: double pi = 3.14141414141414;
boolean : The boolean data type has only two possible values: true and false.
This data type represents one bit of information, but its "size" isn't something that's precisely defined.
Arrays in Java
One-Dimensional Arrays A One-dimensional array is essentially a list of like-typed variables.
Array declaration: type var-name[]; Example: Int student_marks[];
Above example will represent array with no value (null) To link student_marks with actual, physical array of integers, we must allocate one using new keyword. Example:  int student_marks[] = new int[20];
 Multi-Dimensional Arrays In java, Multidimensional arrays are actually array of arrays. Example: int runPerOver[][] = new int[50][6];
Manually allocate different size: Int runPerOver[][] = new int[3][]; runPerOver[0] = new int[6]; runPerOver[1] = new int[7]; runPerOver[2] = new int[6];
Initialization : Int runPerOver[][] = {     {0,4,2,1,0,6},     {1,56,4,1,2,4,0},     {6,4,1,0,2,2},    }


Explain String Class in JAVA. 

An object of the String class represents a string of characters.
The String class belongs to the java.lang package, which does not require an import statement.
Like other classes, String has constructors and methods.
Unlike other classes, String has two operators, + and += (used for concatenation).
Immutability:  Once created, a string cannot be changed: none of its methods changes the string.  Such objects are called immutable.
Immutable objects are convenient because several references can point to the same object safely: there is no danger of changing an object through one reference without the others being aware of the change.
Advantages Of Immutability :Uses less memory.
Disadvantages of Immutability :Less efficient — you need to create a new string and throw away the old one even for small changes.
Empty Strings:
An empty String has no characters.  It’s length is 0. String word1 = ""; String word2 = new String();
Not the same as an uninitialized String (null string) private String errorMsg;
Copy Constructors : Copy constructor creates a copy of an existing String.  Also rarely used.  Not the same as an assignment.
Copy Constructor: Each variable points to a different copy of the String.
Assignment: Both variables point to the same String.
Other Constructors :  Most other constructors take an array as a parameter to create a String.
char[] letters = {‘J’, ‘a’, ‘v’, ‘a’};
String word = new String(letters);
Methods of String Class in JAVA :
char charAt(int index) :function returns the character located at the specified index.
int indexOf(char ch) :Returns the index within this string of the first occurrence of the specified character.
int compareTo(String o) Compares this String to another Object.
String concat(String str) Concatenates the specified string to the end of this string.
int length()  method returns length of the string.
String trim() : method eliminates white spaces before and after string.
String replace(char oc, char nc) : method replaces all occurrence of first sequence of character with second sequence of character.
toUpperCase() : method converts this string into uppercase letter.
toLowerCase() : method into lowercase letter.  

Explain StringBuffer Class in JAVA

The java.lang.StringBuffer class is a thread-safe, mutable sequence of characters. Following are the important points about StringBuffer:
A string buffer is like a String, but can be modified.
It contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.
They are safe for use by multiple threads.
Every string buffer has a capacity.
 Class constructors
1. StringBuffer() This constructs a string buffer with no characters in it and an initial capacity of 16 characters.
2. StringBuffer(CharSequence seq) This constructs a string buffer that contains the same characters as the specified CharSequence.
3. StringBuffer(int capacity) This constructs a string buffer with no characters in it and the specified initial capacity.
4. StringBuffer(String str) This constructs a string buffer initialized to the contents of the specified string.
Here are some of the important methods of StringBuffer class.
append(arg)
delete(start index,end index)
deleteCharAt(index)
insert(index,arg)
replace(start index,end index,arg)
setCharAt(index,newchar) o reverse() 

No comments:

How to crack Aptitude Question In GATE and many Competitive Examination

As you know the aptitude in the competitive examination is quite for more challenging to clear it. But believe me it is the easy stuff t...