Presentation is loading. Please wait.

Presentation is loading. Please wait.

INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The.

Similar presentations


Presentation on theme: "INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The."— Presentation transcript:

1 INTRODUCTION TO JAVA CHAPTER 1 1

2 WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low- level facilities. The Java language is accompanied by a library of extra software that we can use when developing programs. The library provides the ability to create graphics, communicate over networks, and interact with databases. The set of supporting libraries is huge. 2

3 JAVA APPLICATIONS AND APPLETS 3 Applications – standalone Java programs Applets – Java programs that run inside web browsers Java is the first programming language to deliberately embrace the concept of writing programs that. can be executed on the Web.

4 COMPILING JAVA PROGRAMS  The Java compiler produces bytecode (a “.class” file) not machine code from the source code (the “.java” file).  Bytecode is converted into machine code using a Java Interpreter 4

5 PLATFORM INDEPENDENT JAVA PROGRAMS COMPILING  You can run bytecode on any computer that has a Java Interpreter installed 5 “Hello.java”“Hello.class”

6 FUNDAMENTALS 6

7 HELLO WORLD JAVA PROGRAM HELLO WORLD JAVA PROGRAM // import section public class MyFirstprogram { // main method public static void main( String args[] ){ System.out.println(“Hello World”); } // end main } // end class 7

8 SAVING, COMPILING AND RUNNING JAVA PROGRAMS  Saving a Java program :  A file having a name same as the class name should be used to save the program. The extension of this file is ”.java”. “MyFirstprogram.java ”.  Compiling a Java program : Call the Java compiler javac The Java compiler generates a file called” MyFirstprogram.class” (the bytecode).  Running a Java program  Call the Java Virtual Machine java: java MyFirstprogram.class 8

9 COMMENTS IN A JAVA PROGRAM Comments are used to describe what your code does its improve the code readability. The Java compiler ignores them. Comments are made using  // which comments to the end of the line,  /* */, everything inside of it is considered a comment (including multiple lines). Examples: /* This comment begins at this line. This line is included in this comment It ends at this line. */ // This comment starts here and ends at the end of this line. 9

10 GOOD PROGRAMMING PRACTICE Be careful java is a sensitive language. It is good to begin every program with a comment that states the purpose of the program and the author. Every java program consist of at least one class that the programmer define. Begin each class name with capital letter. The class name doesn’t begin with a digit and doesn’t contain a space. Use blank lines and spaces to enhance program readability When you type an opining left brace{ immediately type the closing right brace} Indent the entire body of each class declaration 10

11 PROGRAMS AND DATA  Most programs require the temporary storage of data. The data to be processed is stored in a temporary storage in the computer's memory: space memory.  A space memory has three characteristics Identifier Data Type State 11

12 IDENTIFIER 12 is a sequence of characters that denotes the name of the space memory to be used. This name is unique within a program. Identifier Rules It cannot begin with a digit (0 – 9). It may contain the letters a to z, A to Z, the digits 0 to 9, and the underscore symbol, _. No spaces or punctuation, except the underscore symbol, _, are allowed. Identifiers in Java are case-sensitive. The identifiers myNumber and mynumber, are seen as two different identifiers by the compiler.

13 STATE My be changed  variable All lowercase. Capitalizing the first letter of each word in a multiword identifier, except for the first word. Cannot be changed  constant All uppercase, separating words within a multiword identifier with the underscore symbol, _. 13

14 DATA TYPE The data type defines what kinds of values a space memory is allowed to store. All values stored in the same space memory should be of the same data type. All constants and variables used in a Java program must be defined prior to their use in the program. 14

15 JAVA BUILT-IN DATA TYPES 15

16 PRIMITIVE DATA TYPES 16 Type Size (bits) RangeDescription booleantrue, falseStores a value that is either true or false. char160 to 65535Stores a single 16-bit Unicode character. byte8-128 to +127Stores an integer. short16-32768 to +32767Stores an integer. int32 bits-2,147,483,648 to +2,147,483,647 Stores an integer. long64 bits-9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 Stores an integer. float32 bitsaccurate to 8 significant digitsStores a single-precision floating point number. double64 bitsaccurate to 16 significant digitsStores a double-precision floating point number.

17 VARIABLE/CONSTANT DECLARATION  When the declaration is made, memory space is allocated to store the values of the declared variable or constant.  The declaration of a variable means allocating a space memory which state (value) may change.  The declaration of a constant means allocating a space memory which state (value) cannot change. 17

18 CONSTANT DECLARATION final dataType constIdentifier = literal | expression; final double PI = 3.14159; final int MONTH_IN_YEAR = 12; final short FARADAY_CONSTANT = 23060; final int MAX = 1024; final int MIN = 128; final int AVG = (MAX + MIN) / 2; 18 These are called literals. This is called expression.

19 VARIABLE DECLARATION  A variable may be declared:  With initial value.  Without initial value  Variable declaration with initial value; dataType variableIdentifier = literal | expression; double avg = 0.0; int i = 1; int x =5, y = 7, z = (x+y)*3;  Variable declaration without initial value; dataType variableIdentifier; double avg; int i; 19

20 MORE DECLARATION EXAMPLES  String declaration  with initial value:  String word="Apple";  without initial value:  String word;  char declaration  with initial value:  char symbol ='*';  without initial value:  char symbol; 20  Boolean declaration:  with initial value:  boolean flag=false;  boolean valid=true;  without initial value:  boolean flag;

21 VARIABLES  Float  The default type of floating point numbers is double.  The declaration : float rate = 15.5f ; without the f, the compiler will generate an error 21

22 VARIABLES  Char  When using the char data type, you enclose each character represented within single quotations marks.  Ex: char c = ‘A’ char space = ‘ ‘  Each character is represented by a value (‘A’ is represented by the value 65) 22 char aCharecter='A'; char aAscii =65; System.out.println(aCharecter); System.out.println(aAscii); AAAA

23 23

24 TYPE CONVERSION (CASTING)  Used: to change one data type to another. to avoid implicit type correction.  Syntax: (dataTypeName) expression  Expression evaluated first, then the value is converted to dataTypeName 24

25 TYPE CONVERSION (CASTING)  Examples: 1. (int)(7.9 + 6.7) = 14 2. (int)(7.9) + (int)(6.7) = 13 3. (double)(17) = 17.0 4. (double)(7) /2 = 7.0/2 = 3.5 5. (double)(7/2) = 3.0 6. (int)(7.8+(double)(15)/2) =(int)15.3 =15 25 double x=7.9,y= 6.7; int result; result=(int)(7.9 + 6.7);

26 TYPE CONVERSION (CASTING) 8. (int)(‘A’) = 65 9. (int)(‘8’) = 56 10. (char)(65) = ‘A’ 11. (char) (56) = ‘8’ 26

27 THE CLASS STRING  Contains operations to manipulate strings.  String:  Sequence of zero or more characters.  Enclosed in double quotation marks.  Is processed as a single unit.  Null or empty strings have no characters. “ “  Every character has a relative position, the first character is in position 0. 27

28 Java Programming: From Problem Analysis to Program Design, Second Edition28 THE CLASS STRING  Java system automatically makes the class String available (i.e no need to import this class )  Example : Consider the following declaration : String sentence ; sentence = “programming with java” 28

29 THE CLASS STRING  Length of the string is the number of characters in it.  When determining the length of a string, blanks count.  Example :  “ “  has length = 0  “abc”  has length = 3, position of a = 0,b= 1, c= 2  “a boy”  has length = 5 29

30 Java Programming: From Problem Analysis to Program Design, Second Edition30 SOME COMMONLY USED STRING METHODS String mystr=new String("programming with Java is fun"); System.out.println(mystr); System.out.println(mystr.charAt(3)); System.out.println(mystr.indexOf('J')); System.out.println(mystr.indexOf(‘j')); programming with Java is fun g 17 30

31 Java Programming: From Problem Analysis to Program Design, Second Edition31 SOME COMMONLY USED STRING METHODS 31

32 EXAMPLE String mystr=new String("programming with Java is fun"); System.out.println(mystr); System.out.println(mystr.indexOf('a',10)); System.out.println(mystr.indexOf("with")); System.out.println(mystr.indexOf("ing")); programming with Java is fun 18 12 8 32

33 Java Programming: From Problem Analysis to Program Design, Second Edition33 SOME COMMONLY USED STRING METHODS 33

34 Java Programming: From Problem Analysis to Program Design, Second Edition34 EXAMPLES ON STRING METHODS String s1, s2, s3 ; s1 = “abcdefeg” ; System.out.println( s1.length() ); // 8 System.out.println(s1.charAt(3)); //d System.out.println(s1.indexOf(‘e’)); //4 System.out.println(s1.indexOf(“cd”)); //2 System.out.println(s1.toUpperCase()); //ABCDEFEG 34

35 Java Programming: From Problem Analysis to Program Design, Second Edition35 EXAMPLES ON STRING METHODS System.out.println(s1.substring(1, 4)); //bcd System.out.println(s1 + “xyz”); // abcdefegxyz System.out.println( s1.replace(‘d’,’D’)); // abcDefeg System.out.println(s1.charAt(4) ); // e System.out.println(s1.indexOf(‘b’)); // 1 System.out.println(s1.indexOf(‘e’,5)); // 6 35


Download ppt "INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The."

Similar presentations


Ads by Google