This course aims to provide thorough knowledge of core Java concepts, experience with mock SCJP certification exams, and motivation to take the real Java certification exam. Key topics covered include Java programming fundamentals like classes, objects, methods, and data types. The document also provides details about the SCJP exam format, topics covered in the exam, and how to compile and run basic Java programs.
This course aims to provide thorough knowledge of core Java concepts, experience with mock SCJP certification exams, and motivation to take the real Java certification exam. Key topics covered include Java programming fundamentals like classes, objects, methods, and data types. The document also provides details about the SCJP exam format, topics covered in the exam, and how to compile and run basic Java programs.
This course aims to provide thorough knowledge of core Java concepts, experience with mock SCJP certification exams, and motivation to take the real Java certification exam. Key topics covered include Java programming fundamentals like classes, objects, methods, and data types. The document also provides details about the SCJP exam format, topics covered in the exam, and how to compile and run basic Java programs.
This course aims to provide thorough knowledge of core Java concepts, experience with mock SCJP certification exams, and motivation to take the real Java certification exam. Key topics covered include Java programming fundamentals like classes, objects, methods, and data types. The document also provides details about the SCJP exam format, topics covered in the exam, and how to compile and run basic Java programs.
To get an experience of SCJP exam through mock tests To motivate the students to appear for java certification exam The Java programmer exam covers: Declaration and Access control Flow control and exception handling Garbage collection Language fundamentals Operators and assignments Overloading, Overriding Runtime type and object Orientation Threads The java.awt package---layout The java.lang package The java.util package Briefly about the SCJP2 exam The Programmer exam (SCPJ2) exam is multiple choice examination, testing the candidates knowledge of the java language and its usage To appear for the test, one has to obtain an examination voucher from Sun Educational services after paying the examination fee at any of the registered testing centers. The exam consists of 72 questions which have to be answered in 210 minutes. Immediately after the exam is over, the candidate is indicated whether he/she has passed or failed. Those passing the exam will immediately receive a temporary certificate.
Topics that will be covered today Fundamentals of java programming Various data types and their characteristics Basic structure of a java program Objects and their creation
Basics of Java Programming Classes- Abstraction is an important concept of object oriented languages which is implemented through an entity called Class. Everything in Java must be encapsulated in a class. Thus Java is a truly object oriented language.
Objects- The classes are instantiated through objects. In Java objects are manipulated through object references or simply references. The process of creating objects in Java involves the following steps:
Declaration of reference variable to store the reference to an object
Syntax: classname reference-variable-name
Creating an object This involves use of new operator, together with a call to a method of a class called constructor of class
syntax: reference-variable-name= new class-name()
The new keyword returns a reference to a new instance of a class. Each object has a unique identity and has its own copy of variables declared in the class definition
The declaration and instantiation can also be combined in one statement as:
A a1= new A(10);
A reference provides a handle to the object created and stored in memory. An object can have several references created through aliasing A a1= new A(10); A b1= new A(); b1=a1; // alias
Object reference Keyword new operator Constructor of class A Object After aliasing, the object referenced by b1 initially is free and can be garbage collected Instance members- Each object created will have its own instance of member variables and methods defined in a class. The values of the variables in an object comprise its state. Two distinct objects can have same state if their variables have same values. Static members- There are cases when certain members should only belong to the class, and not be part of any object created from the class. A static variable is initialized when the class is loaded at runtime. Similarly a class can have static methods that belong only to the class and not to any objects of the class. Static variables are declared with keyword static in their declaration. For example: You have a class car and two objects: c1 and c2
C1 has member variables: color No_of_gears C2 also has member variables : color No_of_gears If for C1, color=red and no_of_gear=2, this represents the state of C1 only and has nothing to do with C2. Such variables which are associated with a particular object rather than a class as a whole, are called instance variables as they belong to an instance of car class Similarly, if we have a variable type_of_engine and it holds some value for car class rather than for C1 and C2. Such a variable is called static variable and its value will be same for both the objects. If any object changes the value of static variable, that change will be visible to both the objects
Local variables- Variables that are declared inside a function in a class are called local variables of that function and their scope is limited to that function in which they are declared.
Types of Java programs
Two types of programs are there in Java Standalone programs Applets A standalone Java application is a source code compiled and directly executed. In order to create a standalone application in java, program must define a class with a special method called main(). Applets are java programs that must be run in other applications such as web browsers. The appletviewer provided with JDK can be used to test applets Java Source File Structure A Java source File structure has the following elements specified in the following order: An optional package declaration to indicate the package name in which classes of the program are included. If no package is indicated, all the classes are included in a default package. Zero or more import statements included to import particular package or for importing a particular class of a package. Any number of class and interface definitions. The class and interface can be defined in any order.
Compiling and running a standalone Program Java is a compiled and interpreted language. Thus a program created in java is first compiled by a java compiler. The command to compile a java program is : javac filename.java filename has to be the name of the class with main() function(case-sensitive) After compilation, the machine independent Byte code is created which is represented by a .class file. This byte code is further interpreted by a java interpreter. The interpreter is called the Java Virtual machine or JVM which is contained in JDK. The command to invoke java interpreter is java filename No extensions as it is machine independent code
The java interpreter executes a method called main in class specified on the command line. This is the standard way in which a standalone application is invoked
public static void main(String ar[]){
} The public accessibility with main function signifies that this function is accessible to every class. static denotes that this function belong to class and not to an object. void signifies that main function does not return any value. By default, the main function takes an array of String class objects as argument. The values are given to the array from command line arguments. Key points of a java program There can be any number of classes in a Java program but there can be only one public class that will contain the main function. The name of the Java program has to be the same as the name of the class containing the main function with the extension . java Java is a case sensitive language By default, in Java API(Application programming interface), each class name starts with a capital letter and package name starts with a small letter. Each class in the Java program is compiled into its separate class file Language Building Blocks Like any other programming language, Java language is also defined by grammar rules that specify how syntactically legal constructs can be formed using the language elements. identifiers- A name in a program is called an identifier. Identifiers can be used to denote classes, methods and variables. In Java identifier is composed of a sequence of characters, where each character can be either a letter, a digit , a connecting punctuation (underscore) or any currency symbol and cannot start with a digit
Keywords are reserved identifiers that are predefined in a language and cannot be used to denote other entities.
Keywords in Java Reserved keywords not currently in use const goto Reserved literals in Java null true false
Literals- A literal denotes a constant value.This value can be numerical value (integer or float), a character value, a boolean or a string.In addition there is a null literal to denote the null reference. Integer literals- Integer data types are composed of following primitive types: byte ,short, int and long The default type of an integer literal is int but it can be specified as long by appending the suffix l(or L) to the integer value. No suffix is required for a short or a byte literal Octal and hexadecimal numbers- In addition to decimal number system, integer literals can also be specified in octal(base 8) and hexadecimal(base 16) number systems In Java , octal and hexadecimal numbers are specified with 0 and 0x prefix respecitvely. Floating point literal- Floating point data types come in two flavors: float and double Default type of a floating point literal is double. A floating point literal can be declared as float by adding a suffix f(or F) at the end. Boolean literal- boolean truth values are indicated by keywords true and false. Character literal- A character literal is indicated by enclosing the character in single quotes( ) Escape sequences- Certain escape sequences define special character values String literals-A string literal is a sequence of characters which must be quoted in quotation marks (). White spaces- Sequence of spaces, tabs and line terminator characters. A java program is a free form sequence of characters
Comments- three types of comments are there in Java Single line // Multiple-line /* */ A documentation comment /** */ ---used by javadoc facility to generate HTML documentation of the program Primitive data types- Integer---byte, short, int and long . All are signed integers Character --- char data type represents the character .Represents the symbols in unicode character set. Floating point types: float and double Boolean type- datatype boolean represents the truth values true and false The above given are the atomic data types and are not objects. Each primitive data type has its own wrapper class that can be used to represent the primitive as an object Variable declarations Variables in Java come in three forms Instance variables- That are members of a class and are instantiated for each object of the class. Static variables- Also members of a class but these are not instantiated for any object and therefore belong to the class Local variables( or automatic variables)- declared in methods, in blocks and are instantiated for each invocation of the method or block. Declaration of a variable- A variable stores value of a datatype. int a;
int a=10;
In objects, these variables are called reference variables as they store reference or handle to an object Datatype Name of variable Declaration of variable Declaration and initialization of variable Range of integer/real and character values Float and double data type represents three additional constants. The special value NaN called Not a Number that occurs as the result of undefined mathematical operations. The values POSITIVE_INFINITY and NEGATIVE_INFINITY representing infinity values in positive and negative direction. The range of character is 0 to 2 16 -1 Wrapper classes- Wrapper classes for primitive data types are found in java.lang package. For each primitive data type , there is a corresponsing wrapper class. All the wrapper classes of integers (Byte, Short, Integer, and Long) and for floating point numbers (Float and Double)are subclasses of the java.lang.Number class Initial values for variables- The default values for the member variables of a class are boolean false char \u0000 integer(byte,short,int,long) 0 Floating point(float, double) 0.0F or 0.0D Object reference null Key points for variable initialization Static variables are initialized to default values when class is loaded, if they are not initialized instance variables are also initialized to default values when class is instantiated, if they are not initialized Local variables are not initialized by default and have to be explicitly initialized before use, else the compiler will complain that the variable is not initialized. Local reference variables are also not initialized by default and have to be explicitly initialized before use, else the compiler will throw NullPointerException
A simple Java Program to print hello on screen class A { static int k; int i; public static void main(String ar[]) { int j=12; System.out.println(hello +j); } } Static member variable Non-static member variable Local variable Member method Name of the class A predefined class An object of Printstream class Method of Printstream class String argument of println method