Unit-1 Introduction to Java and Elementary Programming Dr. Rajesh Patel Department of Computer Engineering, SPCE
Machine Language • A computer’s native language. • Differs among different types of computers, is its machine language—a set of built-in primitive instructions. • These instructions are in the form of binary code. • To give a computer an instruction in its native language, have to enter the instruction as binary code.
Contd.. • For example, to add two numbers, you might have to write an instruction in binary code, like this: • 1101101010011010
Assembly Language • Programming in machine language is a tedious process. • Programs written in machine language are very difficult to read and modify. • Assembly language was created in the early days of computing as an alternative to machine languages. • Assembly language uses a short descriptive word, known as a mnemonic, to represent each of the machine-language instructions.
Assembly instruction
High-Level Language • In the 1950s, a new generation of programming languages known as high-level languages emerged. • They are platform independent. • User can write program in a high-level language and run it in different types of machines. • The instructions in a high-level programming language are called statements. • for example, is a high-level language statement that computes the area of a circle with a radius of 5: area = 5 * 5 * 3.14159;
Languages
High level languages • A program written in a high-level language is called a source program or source code. • Because a computer cannot execute a source program, a source program must be translated into machine code for execution. • The translation can be done using another programming tool called an interpreter or a compiler
Interpreter & Compiler • An interpreter reads one statement from the source code, translates it to the machine code or virtual machine code, and then executes it right away. • Statements from the source code may be translated into several machine instructions
Interpreter & Compiler FIGURE (a) An interpreter translates and executes a program one statement at a time. (b) A compiler translates the entire source program into a machine- language file for execution. (a) (b)
Java, the World Wide Web, and Beyond • Java is a powerful and versatile programming language for developing software running on mobile devices, desktop computers, and servers. • Java was developed by a team led by James Gosling at Sun Microsystems. • Sun Microsystems was purchased by Oracle in 2010. • Originally called Oak, Java was designed in 1991 for use in embedded chips in consumer electronic appliances. • In 1995, renamed Java, it was redesigned for developing Web applications.
Contd.. • Java is a full-featured, general-purpose programming language that can be used to develop robust mission- critical applications. • Today, it is employed not only for Web programming but also for developing standalone applications across platforms on servers, desktop computers, and mobile devices.
Contd.. • It was used to develop the code to communicate with and control the robotic rover on Mars • Distributed applications accessed by customers and partners across the Internet • For every new project being developed today, companies are asking how they can use Java to make their work easier
Java Applets • Java initially became attractive because Java programs can be run from a Web browser. Such programs are called applets. • Applets employ a modern graphical interface with buttons, text fields, text areas, radio buttons, and so on, to interact with users on the Web and process their requests. • Applets make the Web responsive, interactive, and fun to use.
HTML • Applets are embedded in an HTML file. • HTML (Hypertext Markup Language) is a simple scripting language for laying out documents, linking documents on the Internet, and bringing images, sound, and video alive on the Web. `
Contd.. • Java is now very popular for developing applications on Web servers. • These applications process data, perform computations, and generate dynamic Web pages.
Java API, JDK, IDE • Java syntax is defined in the Java language specification, and the Java library is defined in the Java API. • The JDK is the software for developing and running Java programs. • An IDE is an integrated development environment for rapidly developing programs
Java Computing Platform  Java Standard Edition (Java SE) to develop client-side applications. The applications can run standalone or as applets running from a Web browser.  Java Enterprise Edition (Java EE) to develop server-side applications, such as Java servlets, JavaServer Pages (JSP), and JavaServer Faces (JSF).  Java Micro Edition (Java ME) to develop applications for mobile devices, such as cell phones.
JDK & Java Development Tools • The JDK consists of a set of separate programs, each invoked from a command line, for developing and testing Java programs. • Instead of using the JDK, - Java development tool (e.g., NetBeans, Eclipse, and TextPad)—software that provides an integrated development environment (IDE) for developing Java programs quickly. • Editing, compiling, building, debugging, and online help are integrated in one graphical user interface.
A Simple Java Program
Contd.. • Reserved words, or keywords, have a specific meaning to the compiler and cannot be used for other purposes in the program. C
Contd..
Contd.. C
Appropriate Comments and Comment Styles • Line comments (beginning with //) • Block comments (beginning with /*), • Java supports comments of a special type, referred to as javadoc comments. • javadoc comments begin with /** and end with */. • They can be extracted into an HTML file using the JDK’s javadoc command.
Programming Errors • Programming errors can be categorized into three types: syntax errors, runtime errors, and logic errors. • Syntax errors result from errors in code construction, such as mistyping a keyword, omitting some • Necessary punctuation, or using an opening brace without a corresponding closing brace
Error
Runtime error Runtime errors are errors that cause a program to terminate abnormally. They occur while a program is running if the environment detects an operation that is impossible to carry out. Input mistakes typically cause runtime errors.
Logic Errors • Logic errors occur when a program does not perform the way it was intended to. • Errors of this kind occur for many different reasons.
Reading Input from the Console • Reading input from the console enables the program to accept input from the user. • Java uses System.out to refer to the standard output device. • System.in to the standard input device.
Read Input • Console input is not directly supported in Java. • Scanner class to create an object to read input from System.in, as follows: Scanner input = new Scanner(System.in); double radius = input.nextDouble(); • The Scanner class is in the java.util package • import java.util.Scanner;
Identifier • Identifiers are the names that identify the elements such as classes, methods, and variables in a program. Example: • ComputeAverage, main, input, number1, number2.
Rules • An identifier is a sequence of characters that consists of letters, digits, underscores(_), and dollar signs ($). • An identifier must start with a letter, an underscore (_), or a dollar sign ($). It cannot start with a digit. • An identifier cannot be a reserved word. • An identifier cannot be true, false, or null. • An identifier can be of any length.
Variables • Variables are used to represent values that may be changed in the program. int count; double radius; double interestRate;
Contd.. int i, j, k; int count = 1; This is equivalent to the next two statements: int count; count = 1; int i = 1, j = 2; C
Named Constants • A named constant is an identifier that represents a permanent value. • The value of a variable may change during the execution of a program, but a named constant, or simply constant, represents permanent data that never changes.
Contd.. final datatype CONSTANTNAME = value; • A constant must be declared and initialized in the same statement. The word final is a Java keyword for declaring a constant. final double PI = 3.14159; Cont
Naming Conventions • Sticking with the Java naming conventions makes your programs easy to read and avoids errors.
Data types
Methods
Numeric Operator
Minutes and Second Program
Numeric Literals • A literal is a constant value that appears directly in a program. • For example, 34 and 0.305 are literals in the following statements: • int numberOfYears = 34; • double weight = 0.305;
Integer Literals • An integer literal can be assigned to an integer variable as long as it can fit into the variable. • A compile error will occur if the literal is too large for the variable to hold. The statement byte b = 128, • for example, will cause a compile error, because 128 cannot be stored in a variable of the byte type.
Contd.. • An integer literal is assumed to be of the int type, whose value is between -231 (-2147483648) and 231 - 1 (2147483647). C
Floating-Point Literals • Floating-point literals are written with a decimal point. • By default, a floating-point literal is treated as a double type value. • For example, 5.0 is considered a double value, not a float value.
Contd.. • For example, you can use 100.2f or • 100.2F for a float number, • 100.2d or 100.2D for a double number. C
Double & Float • The double type values are more accurate than the float type values. • System.out.println("1.0 / 3.0 is " + 1.0 / 3.0); • displays 1.0 / 3.0 is 0.3333333333333333 • System.out.println("1.0F / 3.0F is " + 1.0F / 3.0F); • 1.0F / 3.0F is 0.33333334
Evaluating Expressions and Operator Precedence • Java expressions are evaluated in the same way as arithmetic expressions.
Arithmatic expression
Operator Precedence Rule  Operators within pairs of parentheses are evaluated first. Parentheses can be nested, in which case the expression in the inner parentheses is evaluated first. When more than one operator is used in an expression, the following operator precedence rule is used to determine the order of evaluation.  Multiplication, division, and remainder operators are applied first. If an expression contains several multiplication, division, and remainder operators, they are applied from left to right.  Addition and subtraction operators are applied last. If an expression contains several addition and subtraction operators, they are applied from left to right.
Increment and Decrement Operators • The increment operator (++) and decrement operator (– –) are for incrementing and decrementing a variable by 1. int i = 3, j = 3; i++; // i becomes 4 j——; // j becomes 2
Contd.. • int i = 3, j = 3; • ++i; // i becomes 4 • ——j; // j becomes 2 • double x = 1.0; • double y = 5.0; • double z = x–– + (++y); • After all three lines are executed, y becomes 6.0, z becomes 7.0, and x becomes 0.0. Cn
Contd..
Numeric Type Conversion • Floating-point numbers can be converted into integers using explicit casting. System.out.println((int)1.7); System.out.println((double)1 / 2); Output : 0.5
Contd.. • System.out.println(1 / 2); Output : 0 C
Data Type Conversion
Output

Chapter-introduction about java programming

  • 1.
    Unit-1 Introduction to Javaand Elementary Programming Dr. Rajesh Patel Department of Computer Engineering, SPCE
  • 2.
    Machine Language • Acomputer’s native language. • Differs among different types of computers, is its machine language—a set of built-in primitive instructions. • These instructions are in the form of binary code. • To give a computer an instruction in its native language, have to enter the instruction as binary code.
  • 3.
    Contd.. • For example,to add two numbers, you might have to write an instruction in binary code, like this: • 1101101010011010
  • 4.
    Assembly Language • Programmingin machine language is a tedious process. • Programs written in machine language are very difficult to read and modify. • Assembly language was created in the early days of computing as an alternative to machine languages. • Assembly language uses a short descriptive word, known as a mnemonic, to represent each of the machine-language instructions.
  • 5.
  • 6.
    High-Level Language • Inthe 1950s, a new generation of programming languages known as high-level languages emerged. • They are platform independent. • User can write program in a high-level language and run it in different types of machines. • The instructions in a high-level programming language are called statements. • for example, is a high-level language statement that computes the area of a circle with a radius of 5: area = 5 * 5 * 3.14159;
  • 7.
  • 8.
    High level languages •A program written in a high-level language is called a source program or source code. • Because a computer cannot execute a source program, a source program must be translated into machine code for execution. • The translation can be done using another programming tool called an interpreter or a compiler
  • 9.
    Interpreter & Compiler •An interpreter reads one statement from the source code, translates it to the machine code or virtual machine code, and then executes it right away. • Statements from the source code may be translated into several machine instructions
  • 10.
    Interpreter & Compiler FIGURE(a) An interpreter translates and executes a program one statement at a time. (b) A compiler translates the entire source program into a machine- language file for execution. (a) (b)
  • 11.
    Java, the WorldWide Web, and Beyond • Java is a powerful and versatile programming language for developing software running on mobile devices, desktop computers, and servers. • Java was developed by a team led by James Gosling at Sun Microsystems. • Sun Microsystems was purchased by Oracle in 2010. • Originally called Oak, Java was designed in 1991 for use in embedded chips in consumer electronic appliances. • In 1995, renamed Java, it was redesigned for developing Web applications.
  • 12.
    Contd.. • Java isa full-featured, general-purpose programming language that can be used to develop robust mission- critical applications. • Today, it is employed not only for Web programming but also for developing standalone applications across platforms on servers, desktop computers, and mobile devices.
  • 13.
    Contd.. • It wasused to develop the code to communicate with and control the robotic rover on Mars • Distributed applications accessed by customers and partners across the Internet • For every new project being developed today, companies are asking how they can use Java to make their work easier
  • 14.
    Java Applets • Javainitially became attractive because Java programs can be run from a Web browser. Such programs are called applets. • Applets employ a modern graphical interface with buttons, text fields, text areas, radio buttons, and so on, to interact with users on the Web and process their requests. • Applets make the Web responsive, interactive, and fun to use.
  • 15.
    HTML • Applets areembedded in an HTML file. • HTML (Hypertext Markup Language) is a simple scripting language for laying out documents, linking documents on the Internet, and bringing images, sound, and video alive on the Web. `
  • 16.
    Contd.. • Java isnow very popular for developing applications on Web servers. • These applications process data, perform computations, and generate dynamic Web pages.
  • 17.
    Java API, JDK,IDE • Java syntax is defined in the Java language specification, and the Java library is defined in the Java API. • The JDK is the software for developing and running Java programs. • An IDE is an integrated development environment for rapidly developing programs
  • 18.
    Java Computing Platform Java Standard Edition (Java SE) to develop client-side applications. The applications can run standalone or as applets running from a Web browser.  Java Enterprise Edition (Java EE) to develop server-side applications, such as Java servlets, JavaServer Pages (JSP), and JavaServer Faces (JSF).  Java Micro Edition (Java ME) to develop applications for mobile devices, such as cell phones.
  • 19.
    JDK & JavaDevelopment Tools • The JDK consists of a set of separate programs, each invoked from a command line, for developing and testing Java programs. • Instead of using the JDK, - Java development tool (e.g., NetBeans, Eclipse, and TextPad)—software that provides an integrated development environment (IDE) for developing Java programs quickly. • Editing, compiling, building, debugging, and online help are integrated in one graphical user interface.
  • 20.
  • 21.
    Contd.. • Reserved words,or keywords, have a specific meaning to the compiler and cannot be used for other purposes in the program. C
  • 22.
  • 23.
  • 24.
    Appropriate Comments andComment Styles • Line comments (beginning with //) • Block comments (beginning with /*), • Java supports comments of a special type, referred to as javadoc comments. • javadoc comments begin with /** and end with */. • They can be extracted into an HTML file using the JDK’s javadoc command.
  • 25.
    Programming Errors • Programmingerrors can be categorized into three types: syntax errors, runtime errors, and logic errors. • Syntax errors result from errors in code construction, such as mistyping a keyword, omitting some • Necessary punctuation, or using an opening brace without a corresponding closing brace
  • 26.
  • 27.
    Runtime error Runtime errorsare errors that cause a program to terminate abnormally. They occur while a program is running if the environment detects an operation that is impossible to carry out. Input mistakes typically cause runtime errors.
  • 28.
    Logic Errors • Logicerrors occur when a program does not perform the way it was intended to. • Errors of this kind occur for many different reasons.
  • 29.
    Reading Input fromthe Console • Reading input from the console enables the program to accept input from the user. • Java uses System.out to refer to the standard output device. • System.in to the standard input device.
  • 30.
    Read Input • Consoleinput is not directly supported in Java. • Scanner class to create an object to read input from System.in, as follows: Scanner input = new Scanner(System.in); double radius = input.nextDouble(); • The Scanner class is in the java.util package • import java.util.Scanner;
  • 31.
    Identifier • Identifiers arethe names that identify the elements such as classes, methods, and variables in a program. Example: • ComputeAverage, main, input, number1, number2.
  • 32.
    Rules • An identifieris a sequence of characters that consists of letters, digits, underscores(_), and dollar signs ($). • An identifier must start with a letter, an underscore (_), or a dollar sign ($). It cannot start with a digit. • An identifier cannot be a reserved word. • An identifier cannot be true, false, or null. • An identifier can be of any length.
  • 33.
    Variables • Variables areused to represent values that may be changed in the program. int count; double radius; double interestRate;
  • 34.
    Contd.. int i, j,k; int count = 1; This is equivalent to the next two statements: int count; count = 1; int i = 1, j = 2; C
  • 35.
    Named Constants • Anamed constant is an identifier that represents a permanent value. • The value of a variable may change during the execution of a program, but a named constant, or simply constant, represents permanent data that never changes.
  • 36.
    Contd.. final datatype CONSTANTNAME= value; • A constant must be declared and initialized in the same statement. The word final is a Java keyword for declaring a constant. final double PI = 3.14159; Cont
  • 37.
    Naming Conventions • Stickingwith the Java naming conventions makes your programs easy to read and avoids errors.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
    Numeric Literals • Aliteral is a constant value that appears directly in a program. • For example, 34 and 0.305 are literals in the following statements: • int numberOfYears = 34; • double weight = 0.305;
  • 43.
    Integer Literals • Aninteger literal can be assigned to an integer variable as long as it can fit into the variable. • A compile error will occur if the literal is too large for the variable to hold. The statement byte b = 128, • for example, will cause a compile error, because 128 cannot be stored in a variable of the byte type.
  • 44.
    Contd.. • An integerliteral is assumed to be of the int type, whose value is between -231 (-2147483648) and 231 - 1 (2147483647). C
  • 45.
    Floating-Point Literals • Floating-pointliterals are written with a decimal point. • By default, a floating-point literal is treated as a double type value. • For example, 5.0 is considered a double value, not a float value.
  • 46.
    Contd.. • For example,you can use 100.2f or • 100.2F for a float number, • 100.2d or 100.2D for a double number. C
  • 47.
    Double & Float •The double type values are more accurate than the float type values. • System.out.println("1.0 / 3.0 is " + 1.0 / 3.0); • displays 1.0 / 3.0 is 0.3333333333333333 • System.out.println("1.0F / 3.0F is " + 1.0F / 3.0F); • 1.0F / 3.0F is 0.33333334
  • 48.
    Evaluating Expressions andOperator Precedence • Java expressions are evaluated in the same way as arithmetic expressions.
  • 49.
  • 50.
    Operator Precedence Rule Operators within pairs of parentheses are evaluated first. Parentheses can be nested, in which case the expression in the inner parentheses is evaluated first. When more than one operator is used in an expression, the following operator precedence rule is used to determine the order of evaluation.  Multiplication, division, and remainder operators are applied first. If an expression contains several multiplication, division, and remainder operators, they are applied from left to right.  Addition and subtraction operators are applied last. If an expression contains several addition and subtraction operators, they are applied from left to right.
  • 51.
    Increment and DecrementOperators • The increment operator (++) and decrement operator (– –) are for incrementing and decrementing a variable by 1. int i = 3, j = 3; i++; // i becomes 4 j——; // j becomes 2
  • 52.
    Contd.. • int i= 3, j = 3; • ++i; // i becomes 4 • ——j; // j becomes 2 • double x = 1.0; • double y = 5.0; • double z = x–– + (++y); • After all three lines are executed, y becomes 6.0, z becomes 7.0, and x becomes 0.0. Cn
  • 53.
  • 54.
    Numeric Type Conversion •Floating-point numbers can be converted into integers using explicit casting. System.out.println((int)1.7); System.out.println((double)1 / 2); Output : 0.5
  • 55.
  • 56.
  • 57.

Editor's Notes

  • #19 avaServer Faces It is a server side component based user interface framework. It is used to develop web applications. It provides a well-defined programming model and consists of rich API and tag libraries. The latest version JSF 2 uses Facelets as its default templating system. It is written in Java. The JSF API provides components (inputText, commandButton etc) and helps to manage their states. It also provides server-side validation, data conversion, defining page navigation, provides extensibility, supports for internationalization, accessibility etc.
  • #21 System – is a final class in java.lang package out – is a static member of System class and is object of PrintStream println()- is a method of PrintStream which prints whatever is passed to it on the standard output or your screen. To print we need to call println() method, but we can't call this method directly, we need object of class to which this method belongs. Solution? There is a readymade object of PrintStream class 'out'. So, we should call it as out.print(). But wait a minute…this object is created