SCJP – Quick Notes

LANGUAGE FUNDAMENTALS

INTERFACES

# Top-level interfaces may only be declared public

# Inner interfaces may be declared private and protected BUT only if they are defined in a class otherwise it is a compile error.

# A compile error occurs if the same modifier appears more than once in an interface declaration

# Every interface is implicitly abstract ; the modifier is obsolete and should not be used in new programs

Classes are based on single-inheritance ; they can only extend one class.

Interfaces are allowed multiple-inheritance ; they can extend more than one interface.

# An interface body may contain constant declarations, abstract method declarations, inner classes and inner interfaces

# Fields in an interface are implicitly static and final i.e. they MUST be constants

# Methods in an interface are implicitly abstract and public ; they CANNOT be static

# Methods cannot be declared strictfp, native or synchronized

# Member classes declared in an interface are implicitly public and static

CONSTRUCTORS

  • A constructor can use the access modifiers public, protected or private or have no access modifier (package access)
  • A constructor can not use the modifiers abstract, static, final, native, synchronized or strictfp
  • Constructors are not considered class members , they are not inherited
  • If a class constructor is not declared, a default constructor is supplied by the compiler
  • The default constructor has the same access modifier as the class itself, either: public, protected, private or package (no modifier)
  • To prevent a class from being instantiated outside the class declaration you can create a private constructor.
  • A method having the same name as the class is not treated as a constructor.
  • A constructor cannot have return type.
  • A constructor body can have a return statement provided no value is returned.

MODIFIERS

  • Legal Access Modifiers : – private, public, protected or package (none declared).
  • Legal Special Modifiers : – final, abstract, static, native or synchronized.

METHODS

  • static method is called as class method.
  • non-static method is called instance method.
  • The access modifier of the overloading method should provide at least as much as access as the overloaded method.
  • If return type is void the method should not use return statement with an expression.
  • The parameter list in the method can be declared final. Eq: – myMethod (final int i)
  • A method that overrides another method cannot be declared to throw more checked exceptions than the overridden method.
  • static method cannot use this or super operators in its body.
  • A method declared native or abstract has a semi-colon ) for a body. Don’t use curly braces ( {} ).
  • Any method can throw a Runtime or Error Exception without declaring it in the throws clause.
  • An abstract method cannot be declared in a non-abstract class.

main ()

  • Entry point for all java applications, not required in applets.
  • Must be declared public static void.
  • void must appear before main ().
  • Can also be declared final.
  • main () has only one argument, a String array.
  • The argument variable name can be any legal identifiers. Need not be args.
  • The args array is used to access the command line arguments .
  • An application can have more than one main (), as every class can have a separate main () method.
  • main () is inherited and can be overridden if not declared final .

IDENTIFIERS

  • Variables provide named access to data stored in the memory.
  • Java provides two types of variables: – class or field variables and local or automatic variables .
  • Field variables are declared as class members , they store data pertaining to objects.
  • Valid field modifiers are: – public, private, protected, final, transient, static and volatile .
  • Local variables are variables declared in the body of a method .
  • Valid local modifiers are final.
  • An identifier cannot have the same spelling as a Java keyword, Boolean literal or null literal.
  • Valid identifiers begin with one of the following:
    • A Unicode letter
    • The underscore character ( _ )
    • A dollar sign ( $ )
  • Method and variables can have same names.

KEYWORDS

Keyword type

Keywords

Primitive types boolean, byte, char, double, float, int, long, short
Access modifiers public, private, protected
Special modifiers abstract, final, native, static, strictfp, synchronized, transient, volatile
Control flow if, else, do, while, switch, case, default, for, break, continue
OOP specific class, extends, implements, import, instanceof, interface, new, package, super, this
Exception handling catch, finally, try, throw, throws
Method specific return, void
Unused * const, goto
  • main is not a Java keyword.

DEFAULTS

Type

Default value

boolean

false

byte

0

char

‘\u0000’

short

0

int

0

long

0l

float

0.0f

double

0.0d

Object

null

Array

Based on Array type

  • Field variables are automatically initialized to default values .
  • Local variables are not automatically initialized .
  • Arrays are initialized to the default values of their type when they are created, not declared, even if they are local variables.

ARRAYS

  • Arrays are java objects.
  • All java arrays are technically one-dimensional.
  • Arrays must be indexed by int values or byte, short or char .
  • When arrays are created they are initialized to default values.
  • Array references declared as members are initialized to null but references declared in methods are not initialized.
  • All Arrays are allocated at runtime.
  • Curly braces can only be used in the array declaration statements.
  • You can assign an array a null value but u cannot create an empty array by using a blank index .
  • In the case of multidimensional arrays you can give row dimension without initializing the column dimension.
  • Array index operator [] has the highest precedence.

PRIMITIVE TYPES

Data Type

Bit Size

Range

Min/Max values

Default

boolean n/a true or false n/a false
byte signed 8-bit integer -(2 7 ) to 2 7 -1 -128 to 127 0
char 16-bit Unicode 2.0 character 0 to 2 16 -1 0 to 65,535 \0000
short signed 16-bit integer -(2 15 ) to 2 15 -1 -32,768 to 32,767 0
int signed 32-bit integer -(2 31 ) to 2 31 -1 -2,147,483,648 to 2,147,483,467 0
long signed 64-bit integer -(2 63 ) to 2 63 -1 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 0l
float signed 32-bit floating-point NEGATIVE_INFINITY to POSITIVE_INFINITY Can also have the value NaN (Not a number) 0.0f
double signed 64-bit floating-point NEGATIVE_INFINITY to POSITIVE_INFINITY Can also have the value NaN (Not a number) 0.0d
  • Arithmetic with a floating number will never throw an exception; instead it returns one of the constants values NEGATIVE_INFINITY, POSITIVE_INFINITY or NaN.

NUMERIC LITERALS

  • Should explicitly cast.
  • Floating-point constants are double values unless they are suffixed with or .
  • If or is suffixed it is double value.

CHARACTER LITERALS

  • 16 – BIT Unicode characters.

Esc Char

Unicode Char

Definition

\n

\u000A

newline

\t

\u0009

tab

\b

\u0008

backspace

\r

\u000D

return

\f

\u000C

form feed

\ddd

octal value

  • The compiler translates Unicode characters at the beginning of the compile cycle. Using the Unicode escape characters \u000A for newline and \u000D for return in a String or comment produces a compile-error as they are interpreted, literally, as ‘end-of-line’. Always use the special characters ‘\n’ or ‘\r’.

STRING LITERALS

  • String literals are enclosed in double quotes .
  • If you use octal values in Strings be sure to include zero prefix . Zero prefix is not required if u want to include octal values in character literals.
  • String.intern () method (Returns the canonical representation of the string object) à Very important

A pool of strings, initially empty, is maintained privately by the class String.
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals (Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.
All literal strings and string-valued constant expressions are interned.

  • Literal strings will represent the same reference if they are created
    1. In the same class and in the same package
    2. In different classes within the same package
    3. In different classes in different packages
    4. Using constant expressions computed at compile time
    5. By explicitly using the intern() method and the resulting string is already in the string pool
  • Literal strings will represent different references if they are newly created at runtime

Eg: –

String str1 = “Lions and Tigers and Bears!”;

String str2 = “Lions and Tigers and Bears!”;

String str3 = str2;

String str4 = new String(“Lions and Tigers and Bears!”);

String str5 = ” Oh my!”;

String str6 = “Lions and Tigers and Bears! Oh my!”;

String str7 = str1 + str5;

String str8 = (str1 +” Oh my!”).intern();

Comparison output: str1 == str2 -> true // the str2 literal existed (“interned”)

str1 == str3 -> true // hold the same reference

str1 == str4 -> false // str4 explicitly created

str2 == str3 -> true // hold the same reference

str2 == str4 -> false // str4 explicitly created

str3 == str4 -> false // str4 explicitly created

str6 == str7 -> false // str7 computed at runtime

str6 == str8 -> true // explicit use of intern() at runtime

“Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared.”

CLASS LITERALS

  • Class literals are created by appending .class to the name of a primitive or reference type.

System.out.println(int.class); // output: int

System.out.println(System.class); // output: java.lang.System

Page Visitors: 3088

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *