Friday, July 4, 2008

SCJP Exam - Objective 1.3 Part I

Develop code that declares, initializes, and uses primitives, arrays, enums, and objects as static, instance, and local variables. Also, use legal identifiers for variable names.

Legal identifiers
Technically, legal identifiers must be composed of only Unicode characters, numbers, currency symbols, and connecting characters (like underscores).
  • Identifiers must start with a letter, a currency character ($), or a connecting character such as the underscore ( _ ).
  • Identifiers cannot start with a number!
  • After the first character, identifiers can contain any combination of letters, currency characters, connecting characters, or numbers.
  • There is no limit to the number of characters an identifier can contain.
  • Java keywords can't be used as identifier.
  • Identifiers in Java are case-sensitive.

Java keywords

abstractassertbooleanbreak
bytecasecatchchar
classconstcontinuedefault
dodoubleelseenum
extendsfinalfinallyfloat
forgotoifimplements
importinstanceofintinterface
longnativenewpackage
privateprotectedpublicreturn
shortstaticstrictfpsuper
switchsynchronizedthisthrow
throwstransienttryvoid
volatilewhile


Static variables
  • static variables belong to the class, not the instance
  • static variables can be public, protected, default and private
  • static variables can accessed through class reference
  • access static variables through instance variables is legal, but not a good idea
  • static variables get their default value at declaration
  • static variables declared when the class loader load their class
  • static variables live as long as their class lives
  • static variables can declared and initialized at the same time, except when the initialization can throw exception, in this case use static initialization block:
//Required imports here
...
class ThisClassUseStream {
//Compilation fails, because FileInputStream constructor can throw exception.
static InputStream stream = new FileInputStream("filename");

//Works well, otherStream declared and initialized as null.
static InputStream otherStream;

//static initialization block
static {
try {
otherStream = new FileInputStream("filename");
} catch( Exception ex } { ... }
}
}
  • static initialization block execute top-down
  • static variables are not inherited
  • static variables cannot be serialized

Instance variables
  • instance variables belong to an object instance
  • instance variables can be accessed only through an instance
  • instance variables can be accessed from somewhere in their instance
  • instance variables can be public, protected, default and private
  • instance variable can be inherited
  • private variables are not inherited
  • variables with default visibility are not inherited
  • instance variables are created when the constructor is called
  • instance variable get their default values at declaration

Local variables
  • local variables are created inside a method body
  • local variables live as long as their method is running
  • local variables can be accessed only the method where they was declared
  • local variables don't have default values
  • local variables can be marked only as final

Variables - default values
  • boolean variables: false
  • byte variables: 0
  • char variables: '\u0000'
  • double variables: 0.0D
  • float variables: 0.0F
  • int variables: 0
  • long variables: 0L
  • object references: null
  • short variables: 0
Do not forget, only static and instance variables have default values!

Declare, initialize, use primitives as static, instance and local variable
The Java programming language is strongly-typed, which means that all variables must first be declared before they can be used. This involves stating the variable's type and name, as you've already seen:
int a = 5;
Primitive types
There are 8 primitive type in java:
  • boolean data type has only two possible values: true and false.
  • byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive).
  • char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).
  • double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values isn't important.
  • float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values isn't important.
  • int data type is a 32-bit signed two's complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive).
  • long data type is a 64-bit signed two's complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive).
  • short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive).
Default values of data types are showed above.

Primitive and string literals

Integer literals

There are three ways to represent integer numbers in the Java language:
  • decimal (base 10)
  • octal (base 8)
  • hexadecimal (base 16)

Decimal literals
Decimal representation is the most common in Java, for example:
int a = 1;
long l = 5L;
byte b = 0;
short s = 8;

Octal literals
  • octal literals use only the digits 0 to 7
  • octal literals always begin with 0
  • octal literals can up to 21 digits in an octal number, not including the leading zero
So don't forget,
o77 != 77

because 077 is the octal representation of number 63.
  • some octal literals:
int a = 07 //equals decimal 7
int b = 08 //compilation fails, 8 digit not allowed
int c= 01234 equals decimal 668


Hexadecimal literals
  • hexadecimal numbers are constructed using 16 distinct symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A (or a), B (or b), C (or c), D (or d), E (or e) , F (or f)
  • hexadecimal literals always begin with 0x or 0X
  • hexadecimal numbers can be up to 16, not including the prefix Ox
Floating-point literals
  • floating point literals include the decimal point (.)
  • the left side of decimal point is the integer part
  • the right side of decimal point is the fraction part
  • one of the integer or the fraction part must be declared
  • legal declaration and initialization:
double d1 = .1;
double d2 = 1.;
double d3 = 1.d;
double d4 = .3D;
double _____________ = -.66d;
float f1 = 9.66f;

  • not legal declaration and initialization:
double d3 = .;
float f1 = 0.0;
Boolean literals
  • there are two boolean literals: true and false
Character literals
  • character literals can declared between two single quotes
  • character literals can declared as number
  • legal character literals:
char c1 = 'a';
char c2 = '\u00ff';
char c3 = 32;
char c4 = 04;
char c5 = 0xf;
String literals
  • string literals are declared between two double quote
  • string literal declaration:
String s = "Hello World";
Declaration Example

You can download source code here.

Casting primitives
Casts can be implicit or explicit. An implicit cast means you don't have to write code for the cast; the conversion happens automatically. Typically, an implicit cast happens when you're doing a widening conversion.

Implicit cast
byte b = 5;
int i = b;

Int type is bigger the byte, implicit cast work.

Explicit cast
We need use explicit cast, if we want to assign a bigger value to a smaller type. It can cause loss of procession so explicit cast is required.
Explicit cast look like this:
int a = (int) 5.5; //5.5 is a double, it must be cast to int
byte b = (byte) 128; //128 is too big to store in a byte
byte c = 100;

No comments: