Applet Vs Application

All Java programs can be classified as Applications and Applets. The striking differences are that applications contain main() method where as applets do not. One more is, applications can be executed at DOS prompt and applets in a browser. We can say, an applet is an Internet application. [Source]

Concatenate

Concatenate meas to to link together; unite in a series or chain.

Not to subtract. But to add to the end of something. ie concatenate 1 with 1 = 11

Constructors

A class can contain constructors that, when invoked, make objects according to the class “blueprints”. So a constructor declaration looks like a method declaration, but constructors use the name of their class and have no return type. An example of a constructor (with arguments) is down below:

public Bicycle(int cadenceStart, int speedStart, int GearStart) { gear = gearStart; cadence = cadenceStart; speed = speedStart; }

To make a new object you invoke the constructor with the new operator:

Bicycle roadBike = new Bicycle(5, 11, 17)

No Args Constructor

No Args constructor mean literally that there are no “arguments” or anything in the constructor itself. Let me show you:

Args Constructor:

public Bicycle(int CadenceStart, int SpeedStart, int GearStart) { gear = gearStart; cadence = cadenceStart; speed = speedStart; }

Notice that int startCadence , int startSpeed , and startGear are all arguments in the Bicycle constructor.

No Args Constructor:

public Bicycle() { gear = 1; cadence = 10; speed = 0; }

Notice that there isn’t any arguments in between the parenthesis () .

Immutable

What does it mean to be immutable in Java?

Immutable means that once the constructor for an object has completed the execution, that instance of an object can not be altered.

This is useful because it means that you can pass a reference to the object without worrying that something else is going to change it contents. This is especially useful when you are dealing with concurrency, so that there are no locking issues given that immutable objects never change.

For example:

class Foo { private final String myvar; public Foo(final String initialValue) { this.myvar = initialValue; } public String getValue() { return this.myvar; } }

Foo doesn’t have to worry that the caller to getValue() might change the text in the string.

If you imagine a similar class to Foo , but with a StringBuilder rather than a String as a member, you can see that a caller to getValue() would be able to alter the StringBuilder attribute of a Foo instance. [Source]

How do you make something Immutable?

The following rules define a simple strategy for making immutable objects. Note that not all classes documented as “immutable” follow these rules. Don’t provide “setter” methods — methods that modify fields or objects referred to by fields. Make all fields final and private . Make the class final so nobody extends it and adds mutable fields = Don’t allow subclasses to override methods. The simplest way to do this is to declare the class as final . A more sophisticated approach is to make the constructor private and construct instances in factory methods. If the instance fields include references to mutable objects, don’t allow those objects to be changed: Don’t provide methods that modify the mutable objects.

Don’t share references to the mutable objects. Never store references to external, mutable objects passed to the constructor; if necessary, make copies, and store references to the copies. Similarly, make copies of your internal mutable objects when necessary to avoid returning the originals in your methods. Don’t return objects that are mutable If you have fields of mutable types: Make a copy when receiving the object (in the constructor)

Make a copy or a readonly wrapper when supplying the object to someone (in a getter)

ideally don’t have fields of mutable types at all. Do not extend a mutable class [ Bit-wise vs Logical Immutability Bit-wise immutability means that no field of the class is allowed to change at all. That can be done by only having final fields, having mutable member objects only be accessible to the outside by an bit-wise immutable wrapper and only calling bit-wise immutable methods on mutable member objects. Logical immutability means that its behavior will never change, i.e. that you can’t notice any changes from the outside. For example, when you have an immutable square matrix, and you have to compute it’s determinant, then you might want to store that result for further method calls because computing the determinant is pretty expensive. This is not bit-wise immutable, as you’ll store the determinant only when it’s requested for the first time, but other than performance it can’t be observed from the outside. [Source] Source ] [ Source

Initializing

For variables, Initializing means to assigning a value to a variable for the first time. This can happen either immediately as the variable is made or as some point in the future. For example int x = 5 declares the variable x and initializes it to the value 5. int y declares the variable y but it does not initialize it. It can be initialized at some point in the future by assigning a value to it. In a broader sense, initializing is setting up a starting state. In other words, preparing something for use. This can be something as small as a variable, which must be assigned a value before it can be used. For objects, giving them some initial value, or even your entire program might need to do some setup when it starts. [Source] Non-final fields will be auto-initialized to default values if no initial value is provided. Default values are 0 in case of numerical primitives, false in case of boolean, and null in case of objects. Local or final variables will not be auto-initialized. Static fields are initialized when the class is first referenced in any way, while non-static fields are initialized only when the instance is made. [Source] Initialization Vs Declaration Vs Assignment Assignment: throwing away the old value of a variable and replacing it with a new one Initialization: it’s a special kind of assignment: the first. Before initialization objects have null value and primitive types have default values such as 0 or false. Can be done in conjunction with declaration. Declaration: a declaration states the type of a variable, along with its name. A variable can be declared only once. It is used by the compiler to help programmers avoid mistakes such as assigning string values to integer variables. Before reading or assigning a variable, that variable must have been declared.

Reserved Keywords

In Java, keywords have a special meaning and are a part of the syntax. Reserved Keywords that have a unique meaning to the compiler, and so are reserved for specific purposes only; Reserved Keywords cannot be used in other circumstances. An example of a Reserved Keywords is class ; if the compiler sees the word class , it understands that what comes after the word class is the name of the class. Other examples of Reserved Keywords include public , static , and void .

Return

Note that a method returns to the code that invoked it when it completes running through all statements in the method, reaches the return statement or throws an exception (?). Which ever one of these 3 that occurs first.

So in the body of the method, you can use the return statement to return a value. [Source]

What does the return keyword do in a void method in Java?

At that point, it makes Java exit the method. Once the return statement is executed, the rest of the code will not be executed.

For example, the youWillGetAnError(); statement will not execute when being placed after the return statement.

. . . if (n == 3) { return; youWillGetAnError(); //compiler error here }

It’s generally true that no further code is executed after a return statement. However, if the return statement is in a try or catch block, then the corresponding finally-block will still be executed if present. This may fit better into a try-catch-finally part, but I think I should mention it. [Source]

Get() Fetch()

If you have noticed, a lot of java programs have methods that describe them with get or fetch. The purpose of these methods is to get or fetch a value back to somewhere. In other words, these methods return a value.

To recap, if you want to design a method that gets/fetches/returns a value, the method should have the return keyword inside the method like this:

... // Display total payment System.out.println("The total payment is " + loan.getTotalPayment()); ... // Find total payment public double getTotalPayment() { double totalPayment = getMonthlyPayment() * numberOfYears * 12; return totalPayment;

*The standard/convention is get() , but I use fetch() to remind myself that the return statement is the key for returning a value, not what the method is named.

static

static members (fields and methods) belong to the class instead of an instance of the class.

That means that static members are shared for all of the class, but non-static members are only available for the instance of the class.

It means that only one instance of a static field exists even if you make a million instances of the class or you don’t make any. It will be shared by all instances.

Since static method also don’t belong to a specific instance, they can’t refer to instance members (how would you know which instance Hello class you want to refer to?). static members can only refer to static members. Instance members can access non-static members.

* static members can access instance members through an object reference.

public class Example { private static boolean staticField; private boolean instanceField; public static void main(String[] args) { // a static method can access static fields staticField = true; // a static method can access instance fields through an object reference Example instance = new Example(); instance.instanceField = true; }

this

[1]: Depending on the runtime characteristics, it can be one per ClassLoader or AppDomain or thread, but that is beside the point. [ Source

Version 1: this is a reserved keyword that is used to refer back to the current object.

Version 2: Java’s this keyword is used to refer to other constructors when it’s the first thing that’s called in a constructor.

Each non-static method runs in the context of an object. So if you have a class like this:

public class MyThisTest { private int a; public MyThisTest() { this(42); // calls the other constructor } public MyThisTest(int a) { this.a = a; // assigns the value of the parameter a to the field of the same name } public void frobnicate() { int a = 1; System.out.println(a); // refers to the local variable a System.out.println(this.a); // refers to the field a System.out.println(this); // refers to this entire object } public String toString() { return "MyThisTest a=" + a; // refers to the field a } }

Then calling frobnicate() on new MyThisTest() will print

1 42 MyThisTest a=42

So effectively you use it for multiple things:

clarify that you are talking about a field, when there’s also something else with the same name as a field

refer to the current object as a whole

invoke other constructors of the current class in your constructor [Source]

Let me show you how the “this” keyword passes along what it is referring to:

Super

The super keyword is used to access members of a subclass inherited by the superclass from which the members appear from.

super allows a subclass to access overridden methods and hidden members of its superclass.

allows a subclass to access overridden methods and hidden members of its superclass. super allows the subclass constructor to pass to the superclass constructor, including the constructor’s parameters.

Non-Reserved Keywords

These keywords have a special meaning in the right context. But can also be used as identifiers.

append

append : Appends object at end.

x = [1, 2, 3] x.append([4, 5]) print (x)

gives you: [1, 2, 3, [4, 5]]

extend

extend : Extends list by appending elements from the iterable.

x = [1, 2, 3] x.extend([4, 5]) print (x)

gives you: [1, 2, 3, 4, 5]

Method / Class Relationship

A method is a function that is bound to a class.

So what is a function? A function is a collection of computing statements and so is a procedure. So functions and procedures are the same in most programming languages. But In some languages; a function returns a value, but a procedure does not.

So what is a class? In conventional Object Oriented programming, a class is a collection of data (member variables) bound together with the functions/procedures that act on that data (like member functions or methods).

In conclusion, a method is a collection of computing statements that is bound to a class.

Method / Object / Class Relationship

Method = The task

Invoke a Method on Object = ask the object to perform a task

Object = An instance of class. In other words, class is a template for manufacturing objects. Again in alternative text, class is a blueprint that describes methods and variables in each object. One more time, an object is something, while the class is simply the plans for making that something.

Numeric Data Types

Here are 8 primitive data types in Java:

Byte

A byte is composed of 8 bits. And 8 bits can represent up to 256 combinations. In unsigned binary this is 0-255 but in java primitives use signed binary which allows for negative numbers. The downside is that you can’t go as far in either direction because you are using one of the 8 bits to determine if a number is positive or negative you you only have 7 bits to work with. A signed 8 bit number has a range of -128 to 127 [Source].

Boolean

For True/False logic…

Char

For single Characters…

Double & Float

float is represented in 32 bits, with 1 sign bit, 8 bits of exponent, and 23 bits of the mantissa (or what follows from a scientific-notation number: 2.33728*1012; 33728 is the mantissa).

double is represented in 64 bits, with 1 sign bit, 11 bits of exponent, and 52 bits of mantissa.

By default, Java uses double to represent its floating-point numerals (so a literal 3.14 is typed double ). It’s also the data type that will give you a much larger number range, so I would strongly encourage its use over float .

There may be certain libraries that actually force your usage of float , but in general – unless you can guarantee that your result will be small enough to fit in float ‘s prescribed range, then it’s best to opt with double .

If you require accuracy – for instance, you can’t have a decimal value that is inaccurate (like 1/10 + 2/10 ), or you’re doing anything with currency (for example, representing $10.33 in the system), then use a BigDecimal , which can support an arbitrary amount of precision and handle situations like that elegantly. [Source]

Int

An Integer is composed of 32 bits…

Long

A long is composed of 64 bits….

Short

A short is composed of 16 bits…

Variables

Variables store data values to be used later in the Java program. Variables store values that can be numerical, alphabetical, or an action. Note that Variables are called “variables” because their values can be changed

For example in the code below, the variable radius is initially 1.0 . But then is reassigned the value 2.0 . You can see a similar reassignment of value for the variable area too.

public class A { public static void main(String[] args) { // Compute the first area double radius = 1.0; double area = radius * radius * 3.14; System.out.println("The area is " + area + " for radius " + radius); //Compute the second area radius = 2.0; area = radius * radius * 3.14; System.out.println("The area is" + area + " for radius " + radius); } }

The output shows the change of variable value:

Variables are for representing data of a certain type. So to use a variable, you need to declare it by telling the compiler its name as well as what type of data it can store. The variable declaration tells the compiler to allocate the appropriate memory space for the variable based on its data type. The syntax for declaring a variable is:

datatype variableName;

Examples:

int count; //Declare count to be an integer variable double radius; //Declare radius to be a double variable double interestRate;//Declare interestRate to be a double variable

If variables exist of the same type, they can be declared together like this:

datatype variable1, variable2, ..., variablen;

Notice that the variables are separated by commas. For example:

int i, j, k; //Declare i, j , and k as int variables

Variables usually have initial values. You can declare a variable and initialize it in one step. For example:

int count = 1;

This is equivalent to the next two statements:

int count = 1; count = 1;

Another option you have is at the same time to declare and initialize variables together of the same type on the same line:

int i = 1, j = 2

Assignment & Initialization

Assignment is to throw away an old value of a variable and replace it with a new one.

Initialization is a special kind of assignment. Initialization is the very first assignment for an object. Before initialization objects have null value and primitive types have default values such as 0 or false . Can be done in conjunction with declaration.

Declaration

A declaration states the type of a variable, along with its name. A variable can be declared only once. It is used by the compiler to help programmers avoid mistakes such as assigning string values to integer variables. Before reading or assigning a variable, that variable must have been declared. [Source]

Expression

An expression represents a computation involving values, variables, and operators that, when taking them together, evaluates to a value. And…

A mathematical expression always returns something, but a Java expression doesn’t have to. Another difference is that expressions can, and often do, have side effects in Java. A side effect is pretty much anything that happens other than returning a value. [Source]

Node Vs Element

Concerning the the List Abstract Data Type in Java, the difference between nodes and elements is that an element is the data which is stored in the node. Another feature of the node is that it has a link to the next node. The link itself is not data. Rather, an address is stored as a link, much like a url or web address points to a website from a text link.