User:Sidanshugupta

Intro

 * The single biggest difference between Java and C/C++ is that Java has a pointer model that eliminates the possibility of overwriting memory and corrupting data.
 * Java was designed to make certain kinds of attacks impossible,among them
 * Overrunning the runtime stack—a common attack of worms and viruses
 * Corrupting memory outside its own process space
 * Reading or writing files without permission
 * The compiler generates an architecture-neutral object file format—the compiled code is executable on many processors, given the presence of the Java runtime system. The Java compiler does this by generating bytecode instructions.
 * Interpreting bytecodes is necessarily slower than running machine instructions at full speed, so it isn’t clear that this is even a good idea. However, virtual machines have the option of translating the most frequently executed bytecode sequences into machine code, a process called just-in-time compilation.

Datatypes,variables
int vacationDays; System.out.println(vacationDays); // ERROR--variable not initialized public static final double CM_PER_INCH = 2.54;(as a class constant)
 * You can never use the values of uninitialized variables. For eg:, Java compiler flags the following sequence of statements as an error:
 * In Java, you use the keyword final to denote a constant.eg: final double CM_PER_INCH = 2.54;

import static java.lang.Math.*; System.out.println("The square root of \u03C0 is " + sqrt(PI));
 * Starting with Java SE 5.0, you can avoid the Math prefix for the mathematical methods and constants by adding the following line to the top of your source file:

double x = 9.997; int nx = (int) x;
 * Conversions in which loss of information is possible are done by means of casts. The syntax for casting is to give the target type in parentheses, followed by the variable name. For example:

Strings
StringBuilder builder = new StringBuilder; builder.append(ch); // appends a single character
 * Java does not have a built-in string type. Instead, the standard Java library contains a predefined class called, naturally enough, String. Each quoted string is an instance of the String class:
 * Java, like most programming languages, allows you to use the + sign to join (concatenate) two strings.
 * Follow these steps if you need to build a string from many small pieces. First, construct an empty string builder:


 * StringBuilder class was introduced in JDK 5.0. Its predecessor, StringBuffer, is slightly less efficient, but it allows multiple threads to add or remove characters. If all string editing happens in a single thread (which is usually the case), you should use StringBuilder instead.

Input/Output
Console cons = System.console; String username = cons.readLine("User name: "); char[] passwd = cons.readPassword("Password: ");
 * The Scanner class is not suitable for reading a password from a console since the input is plainly visible to anyone. Java SE 6 introduces a Console class specifically for this purpose. To read a password, use:

Arrays

 * In java.util.Arrays
 * .sort - uses Quick sort
 * .binarySearch - uses Binary search

OOPS

 * The key to making encapsulation(Information Hiding) work is to have methods never directly access instance fields in a class other than their own. Programs should interact with object data only through the object’s methods. Encapsulation is the way to give the object its “black box” behavior, which is the key to reuse and reliability. This means a class may totally change how it stores its data, but as long as it continues to use the same methods to manipulate the data, no other object will know or care.


 * When designing an object-oriented system, there is no “top,” and newcomers to OOP often wonder where to begin. The answer is, you first find classes and then you add methods to each class. A simple rule of thumb in identifying classes is to look for nouns in the problem analysis.Methods, on the other hand, correspond to verbs.


 * The dependence, or “uses–a” relationship, is the most obvious and also the most general.Thus, a class depends on another class if its methods use or manipulate objects of that class.Try to minimize the number of classes that depend on each other. The point is, if a class A is unaware of the existence of a class B, it is also unconcerned about any changes to B!(And this means that changes to B do not introduce bugs into A.) In software engineering terminology, you want to minimize the coupling between classes.

Classes and Objects
has two parts. The expression new Date makes an object of type Date, and its value is a reference to that newly created object. That reference is then stored in the deadline variable.
 * In the Java programming language, you use constructors to construct new instances. A constructor is a special method whose purpose is to construct and initialize objects.
 * It is important to realize that an object variable doesn’t actually contain an object. It only refers to an object.
 * Date deadline = new Date;
 * Always make your instance fields private and mostly the instance methods public
 * Constructors:
 * A constructor has the same name as the class.
 * A class can have more than one constructor.
 * A constructor can take zero, one, or more parameters.
 * A constructor has no return value.
 * A constructor is always called with the new operator.

class Employee{ public Date getHireDay{ return (Date) hireDay.clone; } }
 * If you need to return a reference to a mutable object, you should clone it first. A clone is an exact copy of an object that is stored in a new location.


 * As the method is private, the designers of the class can be assured that it is never used outside the other class operations and can simply drop it.


 * static methods don’t operate on objects, you cannot access instance fields from a static method. But static methods can access the static fields in their class.


 * summary of what you can and cannot do with method parameters in the Java programming language:
 * A method cannot modify a parameter of primitive type (that is, numbers or boolean values).
 * A method can change the state of an object parameter.
 * A method cannot make an object parameter refer to a new object.

Constructors
new ClassName then you must provide a default constructor (with no parameters).
 * you get a free default constructor only when your class has no other constructors. If you write your class with even a single constructor of your own and you want the users of your class to have the ability to create an instance by a call to

public Employee(double s) { // calls Employee(String, double) this("Employee #" + nextId, s); nextId++; }
 * If the first statement of a constructor has the form this(. . .), then the constructor calls another constructor of the same class.

Things added after 5.0

 * java.lang.StringBuilder
 * java.util.Scanner
 * java.io.Console

Useful links

 * 11 buzzwords of Java
 * Good Intro article on Garbage Collection