01 Introduction2Java.pdf
Document Details
Uploaded by GlisteningCoralReef
Full Transcript
Lecture 1 - Introduction Programming language à write computer program à solve problem Problems à involves numeric data (21, 2500.00, etc) and textual data (Mike, Male, 12 Jalan 34, etc) Java Building Elements à variables, constants, data types, operators, and expression An example program that c...
Lecture 1 - Introduction Programming language à write computer program à solve problem Problems à involves numeric data (21, 2500.00, etc) and textual data (Mike, Male, 12 Jalan 34, etc) Java Building Elements à variables, constants, data types, operators, and expression An example program that compute the circumference and area of a circle: public class TestCircle { public static void main(String[] args) { int radius; // radius - variable final double PI = 3.14159; // PI - constants radius = 10; double area = PI * radius * radius; double circumference = 2 * PI * radius; System.out.println(”For a circle with radius ” + radius + ”,”); System.out.print(”The circumference is ” + circumference); System.out.println(” and the area is ” + area); } } TestCircle class name radius, area, circumference Identifiers variable – stores values that varies PI constant – stores fixed value (final) int radius à the data type of the variable is int (it can only store integer value, not floating value) i.e. radius = 10; (okay) but radius = 10.5; (Error!) * is an operator – symbol that represent what we can do onto the data. (* - represents multiplication) other operators include +, -, /, %, >, >=, java TestCircle For a circle with radius 10, The circumference is 62.8318 and the area is 314.159 Rules for naming indentifier I. Begins with letter, $, or _ (underscore); subsequent characters can include digits II. Cannot be reserved words (keywords) à public, class, static, void, int, double, final III. Can be of any length Page 1 of 4 Primitive Data Type & Reference Data Type à numeric > integer: byte, short, int, long è -25, 0, 15, 2345, 12L > floating-point: float, double è -1.5, 0.0, 14.5, 1e5, 1.2f, 2.1F, 1.3d, 1.5D à non-numeric > char è ‘?’, ‘8’, ‘A’, ‘a’, ‘\t’, ‘\u0041’ > boolean è true, false byte 8 short 16 int 32 long 64 float 32 double 64 char 16 Note: (1) All decimal number in Java will be treated as double by default. To make a decimal number a float, appends the letters f or F to that number (2) The initialisation can be another variable or an expression as well, eg. int a = 5; int b = a; int c = 4*a; Different ways of storing integer and floating point number: [* Actual number is stored in binary format] Integer number: 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 = 9.99999999999999 x 1014 á 15 significant digits sign magnitude Floating-point number: = 0.99999999999 x 109999 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 á = 9.9999999999 x 109998 sign mantissa exponent 11 significant digits þ floating-point variable ß integer value eg. double dNum = 12; float fNum = 12L; (ü) ý integer variable ß floating-point value eg. int iNum = 12.5; long lNum = 1.0f; (r) Type Storage Min value Max value byte 8 -128 127 short 16 -32768 32767 int 32 -2,147,483,648 2,147,483,647 long 64 -9,223,372,036,854,775,808 9,223,372,036,854,775,807 float 32 ~ -3.4e+38, with 7 significant digits ~ 3.4e+38, with 7 significant digits double 64 ~ -1.7e+308, with 15 significant digits ~ 1.7e+308, with 15 significant digits The Java numeric primitive types Page 2 of 4 Reference data type – String (sequence of characters) Eg. " " (space) è Do not confuse with character space: ' ' (single quote) "" (empty string) "HELP" (a string with the sequence of characters H, E, L, P) Declaration of reference variable String subjectName = "Java Programming"; (More appropriate is: String subjectName = new String("Java Programming");) Type casting - an operation that converts a value of one data type into a value of another data type, eg. float f = (float) 10.1; int i = (int) f; Operators Arithmetic è + - * / 3+3 (6), 3.5-2.6 (0.9), 2*4.5 (9.0), 5/2 (2), 5/2.0 (2.5) è % (mainly on integers) 5 % 3 (2), 6 % 3 (0) TYPE OF JAVA PROGRAMS 1. Console Application public class ConsoleApp { public static void main(String[] args) { System.out.println("Hello World!"); } } 2. GUI Application import javax.swing.*; public class GuiApp { public static void main(String[] args) { JOptionPane.showMessageDialog(null, "Hello World!", "GUI Application", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } } 3. Applet (Deprecated since Java 18) import java.applet.*; import java.awt.*; public class AppletEg extends Applet { public void paint(Graphics g) { g.drawString("Hello World!", 20, 20); } } Page 3 of 4 Advantages of Java Java is simple Java is partially modelled after C++, but greatly simplified and improved. For instance, pointers and multiple inheritance often make programming complicated. Java replaced multiple inheritance in C++ with a simple structure called interface, and eliminated pointers. Java uses automatic memory allocation and garbage collection, while C++ required the programmer to allocate memory to collect garbage. Java is object-oriented Object-oriented programming models the real world. Everything in the world can be modelled as an object. A circle is an object, a person is an object, and a window’s icon is an object. Java is an object-oriented language. Programming in Java is centred on creating objects, manipulating objects, and making objects working together. An object has properties and behaviours. Properties are described by using data, and behaviours are defined by using methods. Java code is organised into classes. A class is like a template for the objects. An object is a concrete realisation of a class description. The process of creating an object of the class is called instantiation. Java is distributed Distributed computing involves several computers on a network working together. Java is designed to make distributed computing easy. Networking capability is inherently integrated into Java. Writing network programs in Java is like sending and receiving data to and from a file. Java is architecture neutral Java programs are compiled into byte codes, they can run on any platform that supports Java. It isn’t necessary to recompile a Java program to run on a new machine. Java is multi-threaded Multi-threading is the capability for a program to perform several tasks simultaneously within a program. For example, downloading a video file while playing the video would be considered multi- threading. Java has support for multi-threading built into the language. Programming in multi- threaded environment is usually difficult because many things can happen at the same time or in an unpredictable order. Java, however, provides easy-to-use features for synchronisation that make programming easier. Java is secure Java, being an Internet programming language, is used in a newtworked and distributed environment. You can download a Java applet, run it on your computer, and it will not cause damage to your system, because Java implements several security mechanisms to protect your system from being damaged by a stray program. There is no absolute security, however. Security bugs have been recently discovered in Java, but these bugs are subtle and few and have been addressed. Java is statically typed In a Java program, the type of the objects (numbers, characters, arrays, etc.) that are used must be defined. This helps programmer to find potential problems much sooner during compile time. References: An introduction to Java programming, Y.Daniel Liang, Que E & T Hooked on Java, Van Hoff, Shaio, Starbuck, Addison Wesly Page 4 of 4