1Z1 Scja Beta Exam Notes

Share Embed Donate

Short Description

Download 1Z1 Scja Beta Exam Notes...


Java is a strongly typed language as compiler checks all expressions and parameters. All type mismatch must be corrected otherwise compiler will give an error. Primitive Type: there are 8 primitive types. They are also called simple types. Primitive types are not object oriented. Integers: byte (8 bits), short (2 Bytes), Int (4 bytes), long (8 bytes). All are signed i.e. they can contain positive and negative. Note: Java does not support unsigned value: positive values only. Floating Type (Also known as real numbers): Float (4 Bytes), Double (8 Bytes). Single precision Doubles: (Double precision): Characters: (2 Byte): Java uses Unicode to represent characters. Char can be incremented. E.g. char ch1 = ‘X’; cha1++; //value will be changed to Y Booleans: logical values. Boolean controls if, it can be an outcome of relational operator. + operator has higher precedence than > Literals: Integer Literal: Any whole number value is an integer literal. Decimal, octal and hexadecimal numbers. Floating point Literals: floating point literals defaults to Double. Character Literal: Octal: char x = ‘\141’; HEX: char hex = ‘\u0061’ Variables: In java there are 2 variable scopes: defined by class or method scope. Objects declared in the outer scope are visible to inner scope, however reverse is not true. Two variables with the same name and different scope cannot be defined. Type conversion and casting: Assign a value of one type to a variable of another type. If two types are compatible then conversion is automatic. Explicit conversions need to perform on incompatible data types. Widening Conversion: 2 types should be compatible and destination type is larger than source type. Integer and floating types are compatible with each other. Note: Automatic conversion for storing a literal integer constant into variable of type byte, short, long, char. E.g. final int num = 10; char c1 = num; Narrowing Conversion: int value to byte.

Automation type promotion in Expressions: Java automatically promotes each byte, short or char operand to integer when evaluating an expression. class Test { public static void main (String args []) { int n = 12; float dec = 10; double b = 20.45; char ch1 = 88, ch2 = 'Y'; //octal value char oct = '\141'; char hex = '\u0061'; //single quote char qu = '\''; //Automatic conversion for Integer literal final int num = 24; byte b1 = num; short s = num; long l = num; char c1 = num; //compile-time error byte b2 = 50; b2 = b2 * 2; //operands get automatically promoted to int System.out.println (n); System.out.println (dec); System.out.println (b); System.out.println (ch1); ch1++; System.out.println (ch1); System.out.println (oct); System.out.println (hex); System.out.println (qu); System.out.println (b1); System.out.println (c1); System.out.println (l); System.out.println (s); }//end main }

Arrays: Single Dimensional Array class Array { public static void main (String args []) { //one way of declaring array int arr []; arr = new int [5]; //2nd way of declaring array int arr1 [] = new int [5]; //Arrays can be initialized when they are declared int ar [] = {1,2,3,4,5}; //placing value in array for (int i = 0; i < arr.length; i ++) { arr [i] = i * 2; } //displaying values using arrays

for (int j = 0; j < arr.length; j++) { System.out.print ( " " + arr[j] + ","); } }//end main }

Multidimensional Array: You only need to declare rows and not columns. Columns can be declared manually. Columns can be dynamic Operators: char type in java is actually a subset of integer. a = ++b; First increment is executed and then value is assigned to b. a = b++; First value of b is assigned to a then b is incremented. The outcome of relational operator is a Boolean value. Unsigned Right Shift (>>>): shift a zero into the high order bit no matter what the initial value is. IF/case: switch expression must be of type byte, short, int, char; each case value must be a constant, not a variable. The case argument must be final or const. Duplicate case values are not allowed. Case has to be resolved at compile time. If variable is not Boolean if statement wouldn’t compile. You must initialize a constant variable when its declared. Loops: A null statement is syntactically is valid in java. Continue: Good uses of continue are rare. Objective: Select a resource bundle based on locale ResourceBundle is a set of related subclasses that share the same base name. To select the appropriate ResourceBundle, invoke the ResourceBundle.getBundle () method. Package: Java.util; Class: Locale: Locale class provides 3 constructors and one of them is Locale (String language, String country, String variant) Class: ResourceBundle Class: Applets don’t require a main() method. Constructors: Automatic initialization is performed through constructors. No return type. Proper overloaded constructor is based on argument list. A constructor can be of any data type. Note: this can be used inside any method to refer to the current object. Finalization: a specific action can be defined that will occur when an object is just about to reclaim by the garbage collector. protected void finalize () {//finalization code } Overloading Methods: Methods having same name but different parameter list. Overloaded method must change argument list. “One interface, multiple methods”. Polymorphism is supported through method overloading.

Encapsulation: Encapsulation provides access control. Allow access through well defined set of methods. Public: member can be accessed by another code. Private: member can be accessed only by its members of the class. Protected: applied only when inheritance is involved. Default: same as specifying public. NOTE: when no access modifier is specified then by default the member of a class is public within its own package but cannot be accessed outside of package. Static: Instance variable declared as static are normally global variables. They must only access static data or methods. They cannot refer to this or super. String: Every string is an object of type String. String constants are string objects. Once a string object is created its contents cannot be altered. StringBuilder Class: if large number of String concatenation is required then StringBuilder is more efficient. Inheritance: Only 1 super class for any subclass. Java does not support multiple inheritances. Private members cannot be accessed by subclass. Method Overriding: Method having same name and type signature. Overridden methods are another way that java implements “one Interface, multiple methods” aspect of Polymorphism. The compiler looks only at the reference type not the instance type. Animal a = new Horse (); // Animal reference but a horse object NOTE: The overriding method cannot have a more restricted access modifier. Abstract Classes: Syntax: abstract type name (parameter-list). No objects of an abstract class. Package: it’s the first statement in java source file. It is case-sensitive. If you want your element to be seen outside of your current package, but only to classes that subclasses your class directly, then protected is used. Interfaces: It’s an aspect of polymorphism. It supports dynamic method resolution at run-time. Variables are implicitly final and static. Through interface multiple inheritances can be implemented like in C++. The method that implements an interface must be declared public. When we implement an interface, it must be declared public. Interface variables are public, static, final. Interface methods cannot be static. Exception Handling: To manually throw an exception, use the keyword throw. Any code that must be executed after try block is put in finally. Subclass comes first when catching exception otherwise compile

time error will be generated. The flow of execution stops after the throw statement. To create a standard java standard exception object. throw new NullPointerException (“demo”); Exceptions are within java.lang package. Runtime exceptions are unchecked exceptions. Runtime exceptions need not to be included in any method throws list. Exceptions that must be included in method’s throws list are checked exceptions. JDBC API: Objective: Submit Queries and read results from database 1. Establish connection with datasource Connection con = DriverManager.getConnection (); 2. Create a statement: A statement is an interface that represents an SQL statement. Statement objects result in resultSet objects Statement stmt = con.createStatement (); 3. Execute Queries Resultset rs = stmt.executeQuery (query); //where query is query While (rs.next()) //Retrieving fields from database For INSERT, DELETE OR UPDATE use following method executeUpdate () //returns an integer representing number of rows affected. 4. Closing connection Stmt.close () Objective: Declare and use ArrayList ArrayList: Package: java.util.ArrayList. Implement List interface. Decleration: ArrayList al = new ArrayList (); //builds empty array list Note: you cannot store primitive types in collection. toArray() gives you contents of original list.

View more...


Copyright ©2017 KUPDF Inc.