Welcome Back Advanced Java Programming CSE 73455345 NTU

Welcome Back!!! Advanced Java Programming CSE 7345/5345/ NTU 531 Session 2 Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Office Hours: by appt 3: 30 pm-4: 30 pm SIC 353 Chantale Laurent-Rice Welcome Back!!! trice 75447@aol. com claurent@engr. smu. edu Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Introduction • Chapter 1 – Course Objectives – Organization of the Book Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Objectives • Upon completing this chapter, you will understand – Create, compile, and run Java programs – Primitive data types Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Book Chapters • Part I: Fundamentals of Programming – Chapter 1 Introduction to Java – Chapter 2 Primitive Data Types and Operations – Chapter 3 Control Statements – Chapter 4 Methods – Chapter 5 Arrays Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Book Chapters, cont. • Part II: Object-Oriented Programming – Chapter 6 Objects and Classes – Chapter 7 Strings – Chapter 8 Class Inheritance and Interfaces – Chapter 9 Object-Oriented Software Development Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Book Chapters, cont. • Part III: GUI Programming – Chapter 10 Getting Started with GUI Programming – Chapter 11 Creating User Interfaces – Chapter 12 Applets and Advanced GUI Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Book Chapters, cont. • Part IV: Developing Comprehensive Projects – Chapter 13 Exception Handling – Chapter 14 Internationalization – Chapter 15 Multithreading – Chapter 16 Multimedia – Chapter 17 Input and Output – Chapter 18 Networking – Chapter 19 Java Data Structures Liang, Oreilly, Herbert Schildt, Joseph O’Neil

• • • Chapter 1 Objectives: Get an overall perspective of what capabilities and features are encompassed by Java and its development kit. Take a first look at Java syntax. Getting Input from Input Dialog Boxes Style and Documentation Guidelines Liang, Oreilly, Herbert Schildt, Joseph O’Neil

What Is Java? • History • Characteristics of Java Liang, Oreilly, Herbert Schildt, Joseph O’Neil

What is Java? • An Object-Oriented Programming Language developed at Sun Microsystems • A Virtual Machine (run-time environment) that can be embedded in web browsers (e. g. Netscape Navigator, Microsoft Internet Explorer and IBM Web. Explorer) and operating systems. • A set of standardized Class libraries (packages), that support: – Creating graphical user interfaces – Communicating over networks – Controlling multimedia data Liang, Oreilly, Herbert Schildt, Joseph O’Neil

History • James Gosling and Sun Microsystems • Oak • Java, May 20, 1995, Sun World • Hot. Java – The first Java-enabled Web browser • JDK Evolutions • J 2 SE, J 2 ME, and J 2 EE (not mentioned in the book) Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Characteristics of Java • • • Java is simple Object-Oriented Distributed Interpreted Robust • Secure • Architectureneutral • Portable • High-performance • Multithreaded • dynamic Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Java is Simple • Java is not just a language for use with the Internet. • It is a full featured Object-Oriented Programming Language (OOPL). • Java is a bit easier than the popular OOP language C++. • Java uses automatic memory allocation and garbage collection. Liang, Oreilly, Herbert Schildt, Joseph O’Neil

What is Object-Oriented Programming? • Think of OOP as a set of implementation techniques that – Can be done in any programming language – Are very difficult to do in most programming languages – OOP provides great flexibility, modularity, and reusability. Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Java is Distributed • Distributed computing involves several computers working together on a network. • Java’s concurrency features make is unique for developing many interactive and networked applications. Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Java is Interpreted • Java Virtual Machine: – Java is compiled to byte-codes whose target architecture is the Java Virtual machine (JVM). – The virtual machine is embeddable within other environments, e. g. web browser and operating systems. – Utilize a byte-code verifier when reading in byte-codes. The class loader is employed for “classes” loaded over the network (enhances security) Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Java Virtual Machine • JVM Environment Java Source code javac Java byte-code. java. class Liang, Oreilly, Herbert Schildt, Joseph O’Neil java Java VM

Java is Robust • Robust means reliable. • No programming language can ensure complete reliability. • Java puts a lot of emphasis on early checking for possible errors, because Java compilers can detect many problems that would first show up at execution time in other languages. • Java has a runtime exception-handling feature to provide programming support for robustness. Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Java Is Architecture-Neutral • Java is interpreted. • JIT compiler – Just-in-time compilers – This provides • Improved performance • Better match to specific hardware Liang, Oreilly, Herbert Schildt, Joseph O’Neil

JIT Compiler • JIT- takes byte-codes and change it to machine code. JVM Running Applet or Application . class file machine code Liang, Oreilly, Herbert Schildt, Joseph O’Neil J. I. T. Compiler

JIT Compiler • Because of the need for architecture independence, performance tuning must be performed on the client-side. • This client-side compilation is known as Just-In-Time (JIT) compilation. Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Portable, Dynamic, Multithreaded, and Extensible • Java runtime based on architecturally-neutral byte-codes (per class). • Multithreading is a program’s capability to perform several tasks simultaneously. Java Runtime interpreted call Native. dll Liang, Oreilly, Herbert Schildt, Joseph O’Neil loaded classes (byte-codes) . class files

Advantages • Byte-code is a compact machine language form. In Java the target machine is the Java Virtual Machine (VM). • These byte-codes are thus portable across architecture boundaries. • Applets and Applications have “class” files loaded on their behalf in order to execute. Liang, Oreilly, Herbert Schildt, Joseph O’Neil

JDK Versions • • • JDK 1. 02 (1995) JDK 1. 1 (1996) Java 2 SDK v 1. 2 (a. k. a JDK 1. 2, 1998) Java 2 SDK v 1. 3 (a. k. a JDK 1. 3, 2000) Java 2 SDK v 1. 4 (a. k. a JDK 1. 4, 2002) Liang, Oreilly, Herbert Schildt, Joseph O’Neil

JDK Editions • Java Standard Edition (J 2 SE) – J 2 SE can be used to develop client-side standalone applications or applets. • Java Enterprise Edition (J 2 EE) – J 2 EE can be used to develop server-side applications such as Java servlets and Java Server. Pages. • Java Micro Edition (J 2 ME). – J 2 ME can be used to develop applications for mobile devices such as cell phones. This book uses J 2 SE to introduce Java programming. Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Java IDE Tools • Forte by Sun Micro. Systems • Borland JBuilder • Microsoft Visual J++ • Web. Gain Café • IBM Visual Age for Java • IBM Eclipse Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Getting Started with Java Programming • A Simple Java Application • Compiling Programs • Executing Applications Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Example 1. 1 Command Line //This application program prints Welcome //to Java! package chapter 1; public class Welcome { public static void main(String[] args) { System. out. println("Welcome to Java!"); } } Source Liang, Oreilly, Herbert Schildt, Joseph O’Neil Run

Creating and Compiling Programs • On command line – javac file. java Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Executing Applications • On command line – java classname Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Command line Example javac Welcome. java Welcome output: . . . Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Compiling and Running a Program Where are the files stored in the directory? Liang, Oreilly, Herbert Schildt, Joseph O’Neil

There are three forms of comments in Java. 1 -int i = 10; // i is used as a counter 2 The multiline comment /* This is a comment */ This form of comment may also extend over several lines as shown here: /* This is a longer comment that extends over five lines. */ Liang, Oreilly, Herbert Schildt, Joseph O’Neil

There are three forms of comments in Java. 3 - This is the documentation comment. /** This is a Java documentation comment */ The advantage of documentation comments is that tools can extract them from source files and automatically generate documentation for your programs. The JDK has a tool named javadoc that performs this function. Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Package • The second line in the program (package chapter 1; ) specifies a package name, chapter 1, for the class Welcome. • Forte compiles the source code in Welcome. java, generates Welcome. class, and stores Welcome. class in the chapter 1 folder. Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Blocks A pair of braces in a program forms a block that groups components of a program. Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Block Styles Use end-of-line style for braces. Liang, Oreilly, Herbert Schildt, Joseph O’Neil

main Method The main method provides the control of program flow. The Java interpreter executes the application by invoking the main method. The main method looks like this: public static void main(String[] args) { // Statements; } Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Displaying Text in a Message Dialog Box • you can use the show. Message. Dialog method in the JOption. Pane class. • JOption. Pane is one of the many predefined classes in the Java system, which can be reused. Source Liang, Oreilly, Herbert Schildt, Joseph O’Neil Run

The show. Message. Dialog Method JOption. Pane. show. Message. Dialog(null, "Welcome to Java!", "Example 1. 2", JOption. Pane. INFORMATION_MESSAGE)); Liang, Oreilly, Herbert Schildt, Joseph O’Neil

import java. awt. *; import java. awt. event. *; import java. applet. Applet; import java. awt. Graphics; /* */ A simple Java Applet <applet code="First. Applet" width=200 height=200> </applet> public class First. Applet extends Applet { public void paint(Graphics g) { g. draw. String("This is my first applet!", 20, 100); } } See word doc for output. Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Integral Literals • Integral literals may be expressed in decimal, octal, or hexadecimal. • The default is decimal. • To indicate octal, prefix the literal with 0 (zero) • To indicate hexadecimal, prefix the literal with 0 x or 0 X; • the hex digits may be upper-or lowercase. Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Integral Literals For example: The value twenty-eight may be expressed the following ways: 28 034 0 x 1 c 0 x 1 C 0 X 1 c 0 X 1 C Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Integral Literals • By default, an integral literal is a 32 -bit value. • To indicate a long (64 -bit) literal, append the suffix L to the literal expression. • The suffix can be lowercase, but then it looks so much like a one that makes it confusing. Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Literals • Literals are explicit data values that appear in your program. • A literal is a value specified in the program source, as opposed to one determined at runtime. Literals can represent primitive or string variables, and may appear on the right side of assignments or in method calls. You cannot assign a value into a literal, so they cannot appear on the left of an assignment. For example: int x = 25; Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Boolean Literals • The only literals of boolean type are true and false. For example: 1. boolean is. Big = true; 2. boolean is. Little = false; Liang, Oreilly, Herbert Schildt, Joseph O’Neil

char Literals A char literal can be expressed by enclosing the desired character in single quotes, For example: char c = ' w '; Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Chapter 1 Topic Summary • Java is many things – A concurrent object-oriented programming language – A virtual machine and Web-aware runtime – A powerful and stable set of class libraries. Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Chapter 2 Liang, Nutshell Objectives: • Introduce Programming with an Example • Identifiers, Variables, and Constants • Primitive Data Types – byte, short, int, long, float, double, char, boolean • Expressions • Operators, Precedence, Associativity, Operand Evaluation Order: ++, --, *, /, %, +=, -=, *=, /=, %=, ^, &, |, +, -, • Syntax Errors, Runtime Errors, and Logic Errors Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Java Reserved Words or Keywords • • • abstract boolean break byte case catch char class const continue default do double else extends finally float for goto if implements import instanceof interface long native new package Liang, Oreilly, Herbert Schildt, Joseph O’Neil private protected public return short static strictfp super switch synchronized this throws transient try void volatile while

Keyword and identifiers • An identifier is a word used by a programmer to name a variable, method, class, or label. • Keywords and reserved words may not be used as identifiers. • An identifier must begin with a letter, a dollar sign (4), or an underscore (_); subsequent characters may be letter, dollar signs, underscores, or digits. Liang, Oreilly, Herbert Schildt, Joseph O’Neil

• foobar Examples // legal • BIGinterface // legal • $income. After. Expenses // legal • 3_nodes 5 // illegal • !the. Case // illegal Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Using Keyword • Using a keyword as an identifier is a syntax error • Keywords that are reserved, but not used, by Java – const – goto Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Reserved Words • Reserved words or keywords are words that have a specific meaning to the compiler and cannot be used for other purposes in the program. • For example, when the compiler sees the word class, it understands that the word after class is the name for the class. Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Java support eight different basic data type • Type • • character boolean byte short integer long float double Description 16 -bit Unicode character data true/false values 8 -bit signed integer numbers 16 -bit signed integer numbers 32 -bit signed integer numbers 64 -bit signed integer numbers 32 -bit signed floating-point numbers 64 -bit signed floating-point numbers Liang, Oreilly, Herbert Schildt, Joseph O’Neil Keyword char boolean byte short int long float double

Primitive Data Type • Java's Primitive data type boolean char byte short int long float double C++ Simple data type integral floating char float short long int long double long enum unsigned char unsigned short unsigned int unsigned long Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Primitive Data Types and Operations • Type Precision byte short int* long char float double boolean ‘obj-ref’ 8 bits 16 bits 32 bits 64 bits - Liang, Oreilly, Herbert Schildt, Joseph O’Neil Default Value 0 0 u 0000 +0. 0 f +0. 0 d false null

The four signed data types are: v v byte short int long Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Valid Control Character • Valid control character are: – b backspace – t horizontal tab – n linefeed – f formfeed – r carriage return – ” double quote – ’ single quote – \ backslash – “dddd” for Unicode - is 0000 to hex ffff Liang, Oreilly, Herbert Schildt, Joseph O’Neil

• • • • Operator Precedence var++, var-+, - (Unary plus and minus), ++var, --var (type) Casting ! (Not) *, /, % (Multiplication, division, and modulus) +, - (Binary addition and subtraction) <, <=, >, >= (Comparison or Relational) ==, !=; (Equality) & (Unconditional AND) ^ (Exclusive OR) | (Unconditional OR) && (Conditional AND) Short-circuit AND || (Conditional OR) Short-circuit OR =, +=, -=, *=, /=, %= (Assignment operator) Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Bitwise Operators Java defines several bitwise operators which can be applied to the integer types, long, int, short, char, byte These operators act upon the individual bits of their operands. Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Bitwise Operators Operator ~ & | ^ >> >>> << Name Bitwise unary NOT Bitwise AND Bitwise OR Bitwise exclusive OR Shift right zero fill Shift left Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Bitwise Operators Operator &= |= ^= >>>= <<= Name Bitwise AND assignment Bitwise OR assignment Bitwise exclusive OR assignment Shift right zero fill assignment Shift left assignment Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Bitwise NOT ( ~) • Also called the bitwise complement, the unary NOT operator, ~ , • Inverts all the bits of its operand. • Example Number 74 before bitwise NOT -> 01001010 after the NOT operator applied -> 10110101 Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Bitwise AND ( & ) • Produces a 1 bit if both operands are also 1, otherwise a zero is produced. Example Number 74 & 29 -> 8 -> 01001010 -> & 00011101 After -> 0 0 1 000 Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Bitwise OR ( | ) • Combines bits such that if either of the bits in the operands is a 1, then the resultant bit is a 1. A zero if both bits are zeros. Example Number 74 | 29 -> 95 -> 01001010 -> | 00011101 After -> 01 011111 Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Bitwise XOR ( ^ ) • Combines bits such that if exactly one operand is 1, then the result is 1. Otherwise, the result is zero. Example Number 74 | 29 -> 87 -> 01001010 -> ^ 00011101 After -> 01 010111 Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Left shift ( << ) Right shift ( >> ) unsigned right shift ( >>>) • Left shift (>>) shifts all of the bits in a value to the left a specified number of times. • Right shift (>>) shifts all of the bits in a value to the right a specified number of times. • Unsigned right shift (>>>) automatically fills the high-order bits with its previous contents each time a shift occurs. This preserves the sign of the value. Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Relational or Comparisons Operator == != > < >= <= Name Equal to Not equal to Greater than Less than Greater than or equal to Less than or equal to Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Relational Operators The relational operators determine the relationship that one operand has to the other They determine equality and ordering. The result of these operations is a boolean value. Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Boolean Logical Operators Operator & | ^ || && ! Name Logical AND Logical OR Logical XOR (exclusive OR) Short-circuit OR Short-circuit AND Logical unary NOT (inverts) Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Boolean Logical Operators Operator &= |= ^= == != ? : Name AND assignment OR assignment XOR assignment Equal to Not equal to Ternary if-then-else Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Boolean Logical Operators The Boolean Logical operators operate only on boolean operands. All of the binary logical operators combine two boolean values to form a resultant boolean value. Liang, Oreilly, Herbert Schildt, Joseph O’Neil

The boolean Type and Operators boolean lights. On = true; boolean lights. On = false; boolean b = (1 > 2); && (and) • || (or) (1 < x) && (x < 100) (lights. On) || (is. Day. Time) • ! !(is. Stopped) (not) Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Truth Table for Operator ! Operand !Operand true false true Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Truth Table for Operator && Operand 1 Operand 2 Operand 1 && Operand 2 false false true true Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Truth Table for Operator || Operand 1 Operand 2 Operand 1 || Operand 2 false true false true Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Truth Table for Operator ^ Operand 1 Operand 2 Operand 1 ^ Operand 2 false true true false Liang, Oreilly, Herbert Schildt, Joseph O’Neil

The & and | Operators &&: conditional AND operator &: unconditional AND operator ||: conditional OR operator |: unconditional OR operator exp 1 && exp 2 (1 < x) && (x < 100) (1 < x) & (x < 100) Liang, Oreilly, Herbert Schildt, Joseph O’Neil

The & and | Operators If x is 1, what is x after this expression? (x > 1) & (x++ < 10) If x is 1, what is x after this expression? (1 > x) && ( 1 > x++) How about (1 == x) | (10 > x++)? (1 == x) || (10 > x++)? Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Getting Input from Input Dialog Boxes String string = JOption. Pane. show. Input. Dialog( null, “Prompt Message”, “Dialog Title”, JOption. Pane. QUESTION_MESSAGE)); where x is a string for the prompting message and y is a string for the title of the input dialog box. Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Convertting Strings to Doubles To convert a string into a double value, you can use the static parse. Double method in the Double class as follows: double. Value =Double. parse. Double(double. String); where double. String is a numeric string such as “ 123. 45”. Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Read / Work With (Course Links) • • Liang, Nutshell Chapter 3 -4 Life Cycle of Applets List Of Basic Tags Try It Editor Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Chapter 3 Control Statements • Selection Statements –Using if and if. . . else –Nested if Statements –Using switch Statements –Conditional Operator • Repetition Statements –Looping: while, do-while, and for –Nested loops –Using break and continue Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Selection Statements • if Statements • switch Statements • Conditional Operators Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Caution Adding a semicolon at the end of an if clause is a common mistake. if (radius >= 0); Wrong { area = radius*PI; System. out. println( "The area for the circle of radius " + radius + " is " + area); } This mistake is hard to find, because it is not a compilation error or a runtime error, it is a logic error. Liang, Oreilly, Herbert Schildt, Joseph O’Neil

switch Statements switch (year) { case 7: annual. Interest. Rate = 7. 25; break; case 15: annual. Interest. Rate = 8. 50; break; case 30: annual. Interest. Rate = 9. 0; break; default: System. out. println( "Wrong number of years, enter 7, 15, or 30"); } Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Repetitions • while Loops • do-while Loops • for Loops • break and continue Liang, Oreilly, Herbert Schildt, Joseph O’Neil

• Introducing Methods Chapter 4 Methods – Benefits of methods, Declaring Methods, and Calling Methods • Passing Parameters – Pass by Value • Overloading Methods – Ambiguous Invocation • Scope of Local Variables • Method Abstraction • The Math Class Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Introducing Methods A method is a collection of statements that are grouped together to perform an operation. Method Structure Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Methods • A method is essentially a set of program statements. It forms the fundamental unit of execution in Java. Each method exists as part of a class. During the execution of a program, methods may invoke other methods in the same or a different class. No program code can exist outside a method, and no method can exist outside a class. Liang, Oreilly, Herbert Schildt, Joseph O’Neil
![Using Methods For example: public class The. Method { public static void main(String[] args) Using Methods For example: public class The. Method { public static void main(String[] args)](http://slidetodoc.com/presentation_image_h2/6516c47fb3348188d72a1db3a2acc53a/image-93.jpg)
Using Methods For example: public class The. Method { public static void main(String[] args) { System. out. println(“First method”); } } Liang, Oreilly, Herbert Schildt, Joseph O’Neil

All methods are passed by value. • All methods are passed by value. This means that copies of the arguments are provided to a method. • Any changes to those copies are not visible outside the method. • This situation changes when an array or object is passed as an argument. Liang, Oreilly, Herbert Schildt, Joseph O’Neil

call-by-value argument passing • In this case the entire array or object is not actually copied. • Instead, only a copy of the reference is provided. • Therefore, any changes to the array or object are visible outside the method. • However, the reference itself is passed by value. Liang, Oreilly, Herbert Schildt, Joseph O’Neil

call-by-value argument passing • • Method a( ) accepts three arguments: an int array an object reference The value of these arguments are displayed before and after the method call. Liang, Oreilly, Herbert Schildt, Joseph O’Neil

call-by-value argument passing • The key points to note are: • The change to the first argument is not visible to the main( ) method. • The changes to the array and object are visible to the main( ) method. Liang, Oreilly, Herbert Schildt, Joseph O’Neil
![public class Call. By. Value { public static void main(String[] args) { // Initializes public class Call. By. Value { public static void main(String[] args) { // Initializes](http://slidetodoc.com/presentation_image_h2/6516c47fb3348188d72a1db3a2acc53a/image-98.jpg)
public class Call. By. Value { public static void main(String[] args) { // Initializes variables int i = 5; int j[] = { 1, 2, 3, 4, }; String. Buffer sb = new String. Buffer("abcd"); // Display variables display(i, j, sb); // call method a(i, j, sb); } //Display variables again display(i, j, sb); Liang, Oreilly, Herbert Schildt, Joseph O’Neil Example:
![Example (con’t) } public static void a(int i, int j[], String. Buffer sb) { Example (con’t) } public static void a(int i, int j[], String. Buffer sb) {](http://slidetodoc.com/presentation_image_h2/6516c47fb3348188d72a1db3a2acc53a/image-99.jpg)
Example (con’t) } public static void a(int i, int j[], String. Buffer sb) { i = 7; j[0] =11; sb. append("fghi"); } public static void display(int i, int j[], String. Buffer sb) { System. out. println(i); for (int index = 0; index < j. length; index++) System. out. print(j[index] + " "); System. out. println(sb); } Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Methods that return Values. The return type for a method can be used in the Java • The return type for a method can be any type used in the Java programming language, which includes the primitive (or scalar) types int, double, char, and so on, as well as class type (including class types you create). Liang, Oreilly, Herbert Schildt, Joseph O’Neil
![Methods that return values public class Getting. ARaise { public static void main(String[] args) Methods that return values public class Getting. ARaise { public static void main(String[] args)](http://slidetodoc.com/presentation_image_h2/6516c47fb3348188d72a1db3a2acc53a/image-101.jpg)
Methods that return values public class Getting. ARaise { public static void main(String[] args) { double my. Salary = 200. 00; System. out. println("Demonstrating some raises"); } predict. Raise(my. Salary); System. out. println("Demonstrating my salary " + my. Salary); predict. Raise(400. 00); predict. Raise. Given. Increase(600, 800); Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Methods that return values public static void predict. Raise(double money. Amount) { double new. Amount; new. Amount = money. Amount * 1. 10; System. out. println("With raise salary is " + new. Amount); } } public static void predict. Raise. Given. Increase(double money. Amount, double percent. Rate) { double new. Amount; new. Amount = money. Amount * (1 + percent. Rate); System. out. println("With raise predicted given salary is " + new. Amount); } Liang, Oreilly, Herbert Schildt, Joseph O’Neil

Read / Work With (Course Links) • • Liang, Nutshell Chapter 4 -6 Life Cycle of Applets List Of Basic Tags Try It Editor Liang, Oreilly, Herbert Schildt, Joseph O’Neil
- Slides: 103