Sunteți pe pagina 1din 20

Comp414 Advanced Programming

Chapter 01 Introduction to Java


Contents:
Introduction Data types, Variables and Keywords Selection Statements and Loops Methods and Arrays

Chapter 01 Introduction to Java

Comp414 Advanced Programming

Introduction
The Genesis of Java: During the late 1970s and early 1980s, C became the dominant computer programming language. Need for C++: Complexity is the big problem in C programming Once a program exceeds somewhere between 25,000 and 100,000 lines of code, it becomes so complex that it is difficult to grasp as a totality C++ was invented by Bjarne Stroustrup in 1979 C++ is extends of C by adding object-oriented features. World Wide Web and Internet are created one more revolution in the programming: is given the stage for the Java Creation of Java In 1991 the new language is developed by Sun Microsystems. Its named as Java in 1995 Java is the platform-independent language (architecture neutral) Platform-independent language that could be used to produce code that would run on a variety of CPUs under differing environments. Programs are inbuilt with the internet for active programs
Chapter 01 Introduction to Java Comp414 Advanced Programming 3

Cont.,
Java Characteristics Java is simple in that it retains much familiar syntax of C++. It is strongly typed. This means that every thing must have a data type. Java performs its own memory management avoiding the memory leaks that plague programs written in languages like C and C++. Java is completely object oriented. Java is multi-threaded, allowing a program to do more than one thing at a time. Network-Savvy: extensive library of routines for coping with TCP/IP protocols like HTTP and FTP. Secure and Robust: Java is intended for writing programs that are reliable and secure in a variety of ways. Portable and architecture neutral.
Chapter 01 Introduction to Java Comp414 Advanced Programming 4

Data Types
In java there are two categories of data types:
Primitive (Basic/Simple) types: consists of int, float, char and Boolean. Reference types: consists of interfaces, classes, and arrays

Chapter 01 Introduction to Java

Comp414 Advanced Programming

Cont.,
1. Integers:
Java defines four integer types: byte, short, int, and long. All of these are signed, positive and negative values. Java does not support unsigned, positive-only integers. byte: The smallest integer type is byte. This is a signed 8-bit type that has a range from 128 to 127. Byte variables are declared by use of the byte keyword. For example: byte b, c; short: short is a signed 16-bit type. It has a range from 32,768 to 32,767. It is probably the least-used Java type. example of short variable declarations: short b; int: The most commonly used integer type is int. It is a signed 32-bit type that has a range from 2,147,483,648 to 2,147,483,647. In addition to other uses, variables of type int are commonly employed to control loops and to index arrays. int a, b;
Chapter 01 Introduction to Java Comp414 Advanced Programming 6

long: long is a signed 64-bit type and is useful for those occasions where an int type is not large enough to hold the desired value. The range of a long is quite large. long seconds; 2. Floating-Point types: Floating-point numbers, also known as real numbers, are used when evaluating expressions that require fractional precision. There are two kinds of floating-point , float and double. float hightemp; double area; 3. Character and String In C/C++, char is an integer type that is 8 bits wide. This is not the case in Java. Instead, Java uses Unicode to represent characters. Unicode defines a fully international character. In Java char is a 16-bit type. The range of a char is 0 to 65,536. It is used to represent a single character. Character literal is enclosed by single quotation mark char ch1=A String: It represent a string of character. String literal is enclosed by double quotation mark String mess=Welcome to Java; 4. Boolean: Java has a simple type, called boolean, for logical values. It can have only one of 2 possible values, true or false. This is the type returned by all relational operators, such as a < b. boolean b;
Chapter 01 Introduction to Java Comp414 Advanced Programming 7

Cont.,

Variables
The variable is the basic unit of storage in a Java program. A variable is defined by the combination of an identifier, a type, and an optional initializer. Declaring Variable: In Java, all variables must be declared before they can be used. The basic form of a variable declaration is shown here: type identifier [ = value][, identifier [= value] ...] ; The type is one of Javas atomic types, or the name of a class or interface. The identifier is the name of the variable. You can initialize the variable by specifying an equal sign and a value is called constant variable. int a, b, c; // declares three ints, a, b, and c. int d = 3, e, f = 5; //constant variable double pi = 3.14159; // declares an approximation of pi. char x = 'x'; Dynamic declaration: Java allows variables to be initialized dynamically, using any expression valid at the time the variable is declared. double c = Math.sqrt(a * a + b * b);
Chapter 01 Introduction to Java Comp414 Advanced Programming 8

Type Conversion
Assign a value of one type to a variable of another type. If the two types are compatible, then Java will perform the conversion automatically. Automatic conversion: Automatic conversion is following two conditions: The two types are compatible. The destination type is larger than the source type. For example int to long is possible by automatically Incompatible Types: Type conversion between two incompatible types is possible by cast. It has this general form: (target-type) value Int to byte int a; byte b; b = (byte) a; double to int double a; int b; b = (double) a;

Chapter 01 Introduction to Java

Comp414 Advanced Programming

Selection Statement
Java supports two selection statements: if and switch. These statements allow you to control the flow of your programs execution based upon conditions known only during run time. if A simple if statement executes an action if and only if the condition is true. Syntax: if(boolean-exoression) statement;

IfElse statement To take alternative actions when the condition is false, you can use a n if else statement. Syntax: if(boolean-expression) statement for true case; else statement for false case;
Chapter 01 Introduction to Java Comp414 Advanced Programming 10

Cont.,
Nested ifs: A nested if is an if statement that is the target of another if or else. Nested ifs are very common in programming. When you nest ifs, the main thing to remember is that an else statement always refers to the nearest if statement. if(i == 10) { if(j < 20) a = b; if(k > 100) c = d; else a = c; }else a = d; if-else-if Ladder: if(condition) statement; else if(condition) statement; else if(condition) statement; . . else statement;
Chapter 01 Introduction to Java Comp414 Advanced Programming 11

Cont.,
Switch: The switch statement is Javas multiway branch statement. It provides an easy way to dispatch execution to different parts of your code based on the value of an expression. As such, it often provides a better alternative than a large series of if-else-if statements. Syntax: switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; break; ... case valueN: statement(s)N; break; default: statement(s)-for-default; }
Chapter 01 Introduction to Java Comp414 Advanced Programming 12

loops
Javas iteration statements are for, while, and do-while. These statements create what we commonly call loops. A loop is repeatedly executes the same set of instructions until a termination condition is met. While Loop: Syntax: while (condition) { // Loop body Statement(s); } The condition can be any Boolean expression. The body of the loop will be executed as long as the conditional expression is true. When condition becomes false, control passes to the next line of code immediately following the loop. do while Loop do-while loop always executes its body at least once, because its conditional expression is at the bottom of the loop. Its general form is: do { // body of loop } while (condition);
Chapter 01 Introduction to Java Comp414 Advanced Programming 13

Cont.,

For For loop is a powerful and versatile construct. Here is the general form of the for statement: Syntax: for(initialization; condition; iteration) { // body }

It is important to understand that the initialization expression is only executed once. Next, condition is evaluated. This must be a Boolean expression. It usually tests the loop control variable against a target value. If this expression is true, then the body of the loop is executed. If it is false, the loop terminates. Next, the iteration portion of the loop is executed. This is usually an expression that increments or decrements the loop control variable.
Chapter 01 Introduction to Java Comp414 Advanced Programming 14

Methods
Syntax: modifier returnValueType methodName(list of parameters) { // method body; } Method overloading In Java it is possible to define two or more methods within the same class that share the same name, as long as their parameter declarations are different. When this is the case, the methods are said to be overloaded, and the process is referred to as method overloading. Method overloading is one of the ways that Java implements polymorphism. void test() void test(int a) void test(int a, int b) double test(double a)

Chapter 01 Introduction to Java

Comp414 Advanced Programming

15

Array
An array is a group of like-typed variables that are referred to by a common name. Arrays of any type can be created and may have one or more dimensions. A specific element in an array is accessed by its index. One dimensional array: The general form of a one-dimensional array declaration is: type var-name[ ]; double myList[]; In fact, the value of myList is set to null, which represents an array with no value. To link myList with an actual, physical array of integers, you must allocate one using new and assign it to myList. new is a special operator that allocates memory. array-var = new type[size]; myList = new double[10]; But we can make it this two step procedures by One step: type array-var=new type[size] double myList = new int[10];
Processing array: Type one double myList={5.6, 4.5, 3.3, 1.2, 4, 3.3, 3, 4, 99, 1}

Type two 2. for(int i=0; i<10; i++) System.out.println(myList[i]);


Chapter 01 Introduction to Java

Comp414 Advanced Programming

16

Cont.,
Multidimensional array: In Java, multidimensional arrays are actually arrays of arrays. To declare a multidimensional array variable, specify each additional index using another set of square brackets. It used to represents table or matrix. For example: int twoD[][] = new int[4][5]; Processing 2D array: for(i=0; i<4; i++) for(j=0; j<5; j++) { twoD[i][j] = k; k++; }

Chapter 01 Introduction to Java

Comp414 Advanced Programming

17

Access Control
As you know, encapsulation links data with the code that manipulates it. Encapsulation provides another important attribute: access control. you can control what parts of a program can access the members of a class. By controlling access, you can prevent misuse. Javas access specifiers are public, private, and protected. protected applies only when inheritance is involved. public: When a member of a class is modified by the public specifier, then that member can be accessed by any other code. private: When a member of a class is specified as private, then that member can only be accessed by other members of its class.

Chapter 01 Introduction to Java

Comp414 Advanced Programming

18

keywords
There are 49 reserved keywords currently defined in the Java language. These keywords cannot be used as names for a variable, class, or method.

Chapter 01 Introduction to Java

Comp414 Advanced Programming

19

End of Chapter 01
Questions & Doubts

Chapter 01 Introduction to Java

Comp414 Advanced Programming

20

S-ar putea să vă placă și