Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review of Java Language Basics

Similar presentations


Presentation on theme: "Review of Java Language Basics"— Presentation transcript:

1 Review of Java Language Basics
B.Ramamurthy CS114A, CS504 2/22/2019 B.Ramamurthy Copyright, 1996 © Dale Carnegie & Associates, Inc.

2 The Java Language Java is a innovative programming language introduced by Sun MicroSystem Inc. Two distinguishing features of Java are: “Run any where” that supports easy embedding of executable code in web pages. Fully object-oriented: it exemplifies object-orientation through its API (Application Program Interface) 2/22/2019 B.Ramamurthy

3 State of Java Java is a language in its infancy . Be prepared to accept the growing pains: The cycle of obsolescence between versions is less than an year (1.0.2 and 1.1.6) No guarantees for downward compatibility New features and APIs are getting added everyday: it is evolving quite rapidly Development tools are not yet reliable. 2/22/2019 B.Ramamurthy

4 Why Java? Yields very nicely to Object-Oriented design. Ex: classes and inheritance Platform independence (run any where capability) Ex: Applets Tremendous support to application development through its extensive API. Ex: GUI interface Uniform interface to system features. Ex: threads, sockets are classes. 2/22/2019 B.Ramamurthy

5 “Out of Their Minds” “We did not know what we wanted and how to do it. It just sort of grew. The first struggle was over what the language would look like. Then how to parse the expressions….” John Backus (Inventor of FORTRAN, BNF (Backus-Naur Form) and FP, a functional language) 2/22/2019 B.Ramamurthy

6 Generating Executable Code
Editor Type in program Emacs/notepad/wordpad Java Source Code sample.java Compiler javac sample.java Java classes sample.class object code / “byte code” 2/22/2019 B.Ramamurthy

7 Java Environment Object code Java Virtual Machine (JVM) Machine code
Target Machine 2/22/2019 B.Ramamurthy

8 Java Virtual Machine JVM is a layer abstracting the underlying details target machine code. JVM is the enabling technology for the “run any where” feature. It also provides the protection needed for your target machine. But this extra layer impairs efficiency. Sun has answered this problem by introducing Just-in Time (JIT) compilation feature. JIT saves the object code directly into native machine code unless otherwise specified. 2/22/2019 B.Ramamurthy

9 Java API : A Simplistic View
classes (/interfaces) packages methods and data 2/22/2019 B.Ramamurthy

10 Types of Java Programs Java Program Application Applet
Embedded in html file Files: file.java, file.class file.html, browser/viewer Application Stand alone program Files: file.java, file.class 2/22/2019 B.Ramamurthy

11 Program Structure : Application
01/25/98 Program Structure : Application A simple Java application is a class with at least one method called “main” main method must always be defined using the words public, static, and void. main method is where the processing begins in a Java program. main contains statements to be executed. 2/22/2019 B.Ramamurthy 6

12 Example class CountDown { public static void main (String[] args) {
01/25/98 Example class CountDown { public static void main (String[] args) { System.out.print (“Three…”); System.out.print (“Two…”); System.out.print (“One…”); System.out.println (“LiftOff …”); } 2/22/2019 B.Ramamurthy 7

13 ApplicationSyntax Here is an informal syntax for a simple program structure class ClassName { public static void main (String[] args) { // main method … add statements here } Other methods and declarations We will add to this definition as we learn more. 2/22/2019 B.Ramamurthy

14 Java Class In Java, classes are means for describing the properties and capabilities of objects in real life that a problem has to deal with. Properties are referred to as “data declarations” or simply data or data fields. Capabilities are referred to as “methods” 2/22/2019 B.Ramamurthy

15 Class examples Example: class of cars, class of trees Class car:
01/25/98 Class examples Example: class of cars, class of trees Class car: Properties / data/parts of a car: color, number of cylinders, manufacturer Capabilities/ methods/member function : acceleration, anti-lock brake 2/22/2019 B.Ramamurthy 5

16 More Details About Class
Java is case-sensitive. Ex: Bags and bags two distinct names. Semi-colon is a terminator of a statement. A class represents a class of objects. A class contains the data declarations (“parts”) and methods (“behaviors” or “capabilities” ). OO Design: Data Declarations are answers to “What is it made of?” (It has a ____, ____, etc.) Methods are answers to “What can it do?” 2/22/2019 B.Ramamurthy

17 Identifiers All the entities used in a program construction should have a name. These names known as identifiers. An identifier can be composed of letters, underscore ( _ ), digits and dollar symbol ($), but it cannot begin with a digit. Java convention: thirdRock, nextStock, coinValue Begin first word of identifier with lower case, the next words in the identifier with uppercase. 2/22/2019 B.Ramamurthy

18 Primitive Data Types Every data is used in a program should have Name (identifier) and Type. Basic data types supported by Java are: byte, short, int and long to represent whole numbers. float and double to represent real numbers (numbers with fractional components). char to represent single character data. boolean to represent conditions. void - no type 2/22/2019 B.Ramamurthy

19 Variables A variable is an identifier (name) of data whose value may change during the execution of a program. A variable refers to the memory location where the data is stored. Syntax for a variable declaration: DataType VariableName; DataType VariableName = InitialValue; DataType VName1, VName2, VName3; /* multiple variables of the same type*/ 2/22/2019 B.Ramamurthy

20 Constants Constants are identifiers that hold a particular value for the duration of their existence. Syntax: final DataType ConstantName = ConstantValue; Example: final double Pi = ; “final” is a reserved word for indicating that the value is final or constant. 2/22/2019 B.Ramamurthy

21 Assignment Statement Syntax: Variable = expression; Semantics:
Evaluate the expression on the right hand side (RHS) of the = and assign the result to the variable on the left hand side (LHS). The expression on the RHS should be consistent with the type of the variable on the LHS. Expression represents a formula. It could be a constant, variable or combination of variables, constants and operators. EX: Total = Score1 + Score2; 2/22/2019 B.Ramamurthy

22 A Complete (but simple) Application
class Circle { static final double PI = 3.14; public static void main (String[] args) { double Radius = 4.6; double Area, Circumference; Area = PI * Radius *Radius; Circumference = 2.0 * PI * Radius; System.out.println(“Area =“ + Area); System.out.println(“Circumference = “ + Circumference); }} 2/22/2019 B.Ramamurthy

23 Arithmetic Operators Arithmetic operators are defined for both float and integer type data. Binary Operators are those with two (for bi) operators. Unary operators have only one operand. Unary operators are + and - These have the highest precedence. Other operators in the order of precedence: Multiplication *, Division /, Remainder of division %Addition +, Subtraction - Assignment operator = 2/22/2019 B.Ramamurthy

24 Summary Simple application program structure
Elements of Java : identifiers, variables and constants, assignments, input/output statements A complete application example 2/22/2019 B.Ramamurthy


Download ppt "Review of Java Language Basics"

Similar presentations


Ads by Google