BIT 3383 Java Programming Sem 1 Session 2011/12 Chapter 2 JAVA basic
Objective: After this lesson, you should be able to: declare, initialize and use variables according to Java programming language guidelines and coding standards use arithmetic operator and expressions in code use different types of conditional & loop structures
Overview Structure of Java program Identifiers Java Keywords Arithmetic/Expression Control flow/structures
Structure of Java Program Example 2.1 //HelloWorld.java program class HelloWorld { } public static void main(string[] args) { System.out.println( Hello World! );}
Structure of Java Program Consist of two main parts : 1) class All the program is enclosed in class definition (e.g: in Example 2.1, a class called HelloWorld). class is keyword, HelloWorld is class name. Programmer-defined class/ user-defined class. The name of the class is called identifier.
Structure of Java Program 2) main() The body of the program is contained in a method (function) called main(). main() method begins the execution of Java application. public means the method can be called anywhere in the program. static means the method is same for all objects of class. void means the main() does not return value. String args[] represents command-line arguments
Structure of Java Program Other components: Comments Block and scope Identifiers Java Keyword Statement
Comment To documentation purpose To increase program readibility Use three permissible styles: //This is a comment /*This is a comment*/ /** This is a comment*/
Block and scope Sometimes called as compound statement A group of statements bound by braces ({ }) A block must be used in a class definition Block statement can be nested
Block and scope A block is a section of code The concept of scope is tightly linked to blocks Scope refers to how sections of a program (blocks) affect the lifetime of variables Scope is determined by blocks
Identifiers Names given to a variable, class or method A series of characters consisting of letters, digits, underscores and dollar sign ($) Are case-sensitive and have no maximum length
Variable Definition: An item of data named by an identifier. An object stores its state in variables. You must explicitly provide a name and a type for each variable you want to use in your program. Variable name is used to refer to the data that the variable contains. The variable's type determines what values it can hold and what operations can be performed on it.
Variable Three types of variables in Java : instance variable, class variable and local variable. Instance variable define attribute or state for the particular object Class variable define attribute/state for all class s object/instance. Local variable declared and used inside method definitions.
Variable: DO & DON T It must be a legal identifier and begins with a letter. It must not be a keyword, a boolean literal (true or false), or the reserved word null. It must be unique within its scope. Variable names begin with a lowercase letter, and class names begin with an uppercase letter.
Variable: DO & DON T If a variable name consists of more than one word, the words are joined together, and each word after the first begins with an uppercase letter, like this: isvisible. The underscore character (_) is acceptable anywhere in a name, but by convention is used only to separate words in constants (because constants are all caps by convention and thus cannot be case-delimited)
Variable Operation Declaring variable : Consists of a type & variable name Format : type variable name Example : int number; Assigning/Initializing value to variable int number = 5; number = nombor; MyDate my_birth = new MyDate; Accessing variable (input statement,object invocation)
Data Types Define the storage methods available for storing Every variable must have a data type. A variable's data type determines the values that the variable can contain and the operations that can be performed on it. Java data types : simple and composite Simple data types: integer, floating point, boolean, character types Composite : string, arrays, classes.
Data type :Integer Can be both positive, negative and value 0. Type byte short int long Size 8 bits 16 bits 32 bits 64 bits Example declaration: int i; short rocketfuel; long angle, magnitude; byte red, green, blue;
Data type :Floating point Represent numbers with both whole numbers and fractional part. Types of floating point : double and float Type float double Example declaration: double number; float number; Size 32 bits 64 bits
Data type :Character Symbol that is used in text. char data types is 16 bits. Declare a char value like this: char alphabet or char alpha = a ; Stores only single character
Data type :Character Special character literals Character Backslash Backspace Line feed Tab Symbol \\ \b \n \t Example: char backspace = \b ;
Data type :Boolean A way to define if specific condition has been met. Represented in Java by boolean data type Two possible values : true /false Example : boolean value; boolean value = true;
String:data type & literal String a combination of characters Consists of a series of characters inside double quotes Handled by special class calls String Example : Hi! I am a string literals Can contains character constant "A string with a \t tab in it"
Constant constant data that has value which never change Used final to represent constant value Example : final float SALESTAX = 0.06;
Java Language Keywords Predefined identifiers reserved by Java for specific purpose. Java has richer set of keywords than C or C++ Keywords are reserved words. Cannot use any of these words as names in Java programs. true, false, and null are reserved words, so cannot use them as names in programs either.
Java Language Keywords while while while while static static static static instanceof instanceof instanceof instanceof do do do do volatile volatile volatile volatile short short short short import import import import default default default default void void void void return return return return implements implements implements implements continue continue continue continue try try try try public public public public if if if if const const const const * transient transient transient transient protected protected protected protected goto goto goto goto * class class class class throws throws throws throws private private private private for for for for char char char char throw throw throw throw package package package package float float float float catch catch catch catch this this this this new new new new finally finally finally finally case case case case synchronized synchronized synchronized synchronized native native native native final final final final byte byte byte byte switch switch switch switch long long long long extends extends extends extends break break break break super super super super interface interface interface interface else else else else boolean boolean boolean boolean strictfp strictfp strictfp strictfp ** int int int int double double double double abstract abstract abstract abstract
Valid/Invalid Identifiers Valid HelloWorld Hi_Mom tall poundage Invalid Hello World (uses a space) Hi Mom! (uses a space and punctuation mark) short (this is a Java keyword) #age (cannot begin with a letter)
Statement Input and output statement Output statement : System.out.println ( Hello World! ); System.out.print( Hello World! );