You are on page 1of 5

JAVA

1.

Why local variable should be initialized??


Basically, requiring a variable to be assigned a value before you read it is a Good Thing. It means you won't accidentally read something you didn't
intend to. Yes, variables could have default values - but isn't it better for the compiler to be able to catch your bug instead, if it can prove that you're
trying to read something which might not have been assigned yet? If you want to give a local variable a default value, you can always assign that
explicitly.
Now that's fine for local variables - but for instance and static variables, the compiler has no way of knowing the order in which methods will be called.
Will a property "setter" be called before the "getter"? It has no way of knowing, so it has no way of alerting you to the danger. That's why default
values are used for instance/static variables - at least then you'll get a known value (0, false, null etc) instead of just "whatever happened to be in
memory at the time." (It also removes the potential security issue of reading sensitive data which hadn't been explicitly wiped.)

2.

Why static can access only static members ?


A non-static member belongs to an instance. It's meaningless without somehow resolving which instance of a class you are talking about. In a static
context, you don't have an instance, that's why you can't access a non-static member without explicitly mentioning an object reference.
In fact, you can access a non-static member in a static context by specifying the object reference explicitly:
class HelloWorld {
int i;
public HelloWorld(int i) { this.i = i; }
public static void Print(HelloWorld instance) {
Console.WriteLine(instance.i);
}
}
var test = new HelloWorld(1);
var test2 = new HelloWorld(2);
HelloWorld.Print(test);

Without explicitly referring to the instance in the Print method, how would it know it should print 1 and not 2?

3.

why there can be only one public class per source code file

This restriction is not yet enforced by the compiler, although it's necessary for efficient package importation"
It's pretty obvious - like most things are once you know the design reasons - the compiler would have to make an additional pass through all the compilation units
(.java files) to figure out what classes were where, and that would make the compilation even slower.

4.

Access Control Modifiers


Visible to the package, the default. No modifiers are needed.

Visible to the class only (private).

Visible to the world (public).

Visible to the package and all subclasses (protected).

5.

Difference between int[] array and int array[]

The [] is part of the TYPE, not of the NAME.


int[] a, b; // Both a and b are arrays of type int
int c[], d; // WARNING: c is an array, but d is just a regular int

6.
7.

Whenever a method is called with conflicts , it will look for best and clear and
simple match

Local variables are implemented at stack level internally.


Converting primitive data types into object is called boxing,

Simple date format

import java.util.*;
import java.text.*;

public class DateDemo {

public static void main(String args[]) {


Date dNow = new Date( );
SimpleDateFormat ft =
new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");

System.out.println("Current Date: " + ft.format(dNow));


}
}
Current Date: Sun 2004.07.18 at 04:14:09 PM PDT

Regular Expression Syntax

Pattern r = Pattern.compile(pattern);

// Now create matcher object.


Matcher m = r.matcher(line);
if (m.find( )) {
System.out.println("Found value: " + m.group(0) );
System.out.println("Found value: " + m.group(1) );
System.out.println("Found value: " + m.group(2) );
}

Here is the table listing down all the regular expression metacharacter
syntax available in Java
Subexpression

Matches

Matches the beginning of the line.

Matches the end of the line.

Matches any single character except newline. Using m option allows


it to match the newline as well.

[...]

[^...]

Matches any single character in brackets.

Matches any single character not in brackets.

\A

Beginning of the entire string.

\z

End of the entire string.

\Z

End of the entire string except allowable final line terminator.

re*

Matches 0 or more occurrences of the preceding expression.

re+

Matches 1 or more of the previous thing.

re?

Matches 0 or 1 occurrence of the preceding expression.

re{ n}

Matches exactly n number of occurrences of the preceding


expression.

re{ n,}

Matches n or more occurrences of the preceding expression.

re{ n, m}

a| b

Matches at least n and at most m occurrences of the preceding


expression.

Matches either a or b.

(re)

Groups regular expressions and remembers the matched text.

(?: re)

Groups regular expressions without remembering the matched text.

(?> re)

Matches the independent pattern without backtracking.

\w

Matches the word characters.

\W

Matches the nonword characters.

\s

Matches the whitespace. Equivalent to [\t\n\r\f].

\S

Matches the nonwhitespace.

\d

Matches the digits. Equivalent to [0-9].

\D

Matches the nondigits.

\A

Matches the beginning of the string.

\Z

Matches the end of the string. If a newline exists, it matches just


before newline.

\z

Matches the end of the string.

\G

Matches the point where the last match finished.

\n

Back-reference to capture group number "n".

\b

Matches the word boundaries when outside the brackets. Matches


the backspace (0x08) when inside the brackets.

\B

Matches the nonword boundaries.

\n, \t, etc.

Matches newlines, carriage returns, tabs, etc.

\Q

Escape (quote) all characters up to \E.

\E

Ends quoting begun with \Q.

The finalize( ) Method


It is possible to define a method that will be called just before an object's
final destruction by the garbage collector. This method is called finalize( ),
and it can be used to ensure that an object terminates cleanly.

Bubble sort We are sending the biggest number to the end by comparing
two numbers and starting the loop again from 0.
Insertion sort-Like playing card. We will pick a card and place it after
comparing with the pack of card we had.

You might also like