Introduction to Java Programming PDF
Document Details

Uploaded by EfficientRaleigh5829
University of the East
Tags
Summary
This document provides an introduction to Java programming, covering basic elements such as identifiers, keywords, data types, and operators. It is designed to help learners understand fundamental Java concepts. The content includes examples and explanations to aid comprehension of Java's programming principles.
Full Transcript
Introduction to Java Programming “ 2 OBJECTIVES: At the end of the lesson, the learner will be able to: ▪ Identify and learn what are Reserved Words, Identifiers, Data Types, Operators, Assignment Statements, Expressions, Class String, Packages, Classes,...
Introduction to Java Programming “ 2 OBJECTIVES: At the end of the lesson, the learner will be able to: ▪ Identify and learn what are Reserved Words, Identifiers, Data Types, Operators, Assignment Statements, Expressions, Class String, Packages, Classes, Methods, and the Import statement. 3 1. INTRODUCTION TO JAVA All about JAVA : History, Characteristics and Program Types HISTORY OF JAVA ▪ The Java programming language is an object-oriented language that is used both for Web-based Internet and general-purpose programs. Key attributes of the language are in security features and it's architecturally neutral (platform independent). ▪ Java began life as the programming language Oak. The members of the Green Project, which included James Gosling, Patrick Naughton, and Mike Sheridan, developed Oak in 1991. ▪ The chief programmer of Sun Microsystems, James Gosling, was given the task of creating the software for controlling consumer electronic devices. The team wanted a fundamentally new way of computing, based on the power of networks, and wanted it to run on different kinds of computer platforms. Patenting issues gave a new name to Oak – Java. 5 CHARACTERISTICS OF JAVA ▪ Simple – it is easy to learn Java ▪ Object-oriented – everything in Java is in the form of classes and objects. It provides the features of abstraction, encapsulation, polymorphism, and inheritance. ▪ Distributed – Java programs can access data across a network. ▪ Compiled and interpreted – the Java code you write is compiled to bytecode and interpreted when you execute the program. ▪ Robust – Java programs are less prone to error. 6 CHARACTERISTICS OF JAVA ▪ Architecture neutral and portable – the bytecode can be executed on a variety of computers running on different operating systems. ▪ Secure – Java does not allow a programmer to manipulate the memory of the system. ▪ A high performance programming language – Java programs are faster when compared to programs written in other interpreter- based languages. ▪ Multithreaded – it allows multiple parts of a program to run simultaneously. ▪ Dynamic – maintaining different versions of an application is very easy in Java. 7 JAVA PROGRAM TYPES You can write programs using Java: ▪ Applets are programs that are embedded in a Web Page. ▪ Servlets – these programs extend the functionality of Web servers. ▪ Java Applications are stand-alone programs. Java Applications can be further subdivided into: ▫ Console applications – which support character or text output to a computer screen ▫ Windowed Application – which create a GUI with elements such as menus, toolbars and dialog boxes. 8 2. BASIC ELEMENTS OF JAVA All about JAVA : Identifiers, Keywords, Data Types and Operators JAVA IDENTIFIER ▪ A Java identifier must begin with a letter of the English alphabet, a non-English letter (such as α or π), an underscore, or a dollar sign. A class name cannot begin with a digit. ▪ A Java identifier can contain only letters, digits, underscores (_), or dollar signs ($). ▪ A Java identifier cannot be a reserved keyword, such as public or class. ▪ Java identifier cannot be one of the following values: true, false, or null. These are not keywords (they are primitive values), but they are reserved and cannot be used. 10 JAVA IDENTIFIER - Variable Variable - is a named memory location that you can use to store a value. A variable can hold only one value at a time, but the value it holds can change. For example, if you create a variable named ovenTemperature, it might hold 0 when the application starts, later be altered to hold 350, and still later be altered to hold 400. ▪ An item’s data type describes the type of data that can be stored there, how much memory the item occupies, and what types of operations can be performed on the data. Java provides for eight primitive types of data. 11 Declaring a Variable A variable declaration is a statement that reserves a named memory location and includes the following: A data type that identifies the type of data that the variable will store An identifier that is the variable’s name An optional assignment operator and assigned value, if you want a variable to contain an initial value An ending semicolon Example: int myAge = 25; or int yourAge; yourAge = 30; 12 JAVA IDENTIFIER - Constant Constant – when its value cannot be changed while a program is running; a data item is variable when its value might change. For example, when you include the following statement in a Java class, the number 459 is a constant: System.out.println(459); ▪ Every time an application containing the constant 459 is executed, the value 459 is displayed. Programmers refer to the number 459 as a literal constant because its value is taken literally at each use. The number 459 is also a numeric constant as opposed to a character or string constant. Additionally, it is an unnamed constant as opposed to a named one, because no identifier is associated with it. 13 Declaring Constant A named constant differs from a variable in several ways: In its declaration statement, the data type of a named constant is preceded by the keyword final. A named constant can be assigned a value only once, and then it can never be changed. Usually you initialize a named constant when you declare it; if you do not initialize the constant at declaration, it is known as a blank final, and you can assign a value later. You can assign a value to a final variable only once, and you must assign a value before the variable is used. Although it is not a requirement, named constants conventionally are given identifiers using all uppercase letters, using underscores as needed to separate words. Example: final int NUMBER_OF_DEPTS = 20; final double PI = 3.14159; final double TAX_RATE = 0.015; final string COMPANY = "ABC Manufacturing"; 14 JAVA KEYWORDS 15 JAVA DATA TYPES The data types in Java are classified as: ▫ Primitive or standard data types ▫ Abstract or derived data types 16 JAVA DATA TYPES - Primitive ▪ Primitive data types, also known as standard data types, are the data types that are built into he Java language. ▪ The Java compiler contains detailed instructions on each legal operation supported by the data type. ▪ There are eight primitive data types in Java: byte, short, int, long, float, double, char, and Boolean. 17 JAVA DATA TYPES - Primitive Data Type Size/Format Description Range (Numbers) byte 8-bit Byte-length integer -128 to +127 if signed & 0 to 255 if unsigned short 16-bit Short integer -32768 to +32767 int 32-bit Integer -231 to 231 -1 long 64-bit Long integer -263 to 263 -1 float 32-bit Single-precision floating point +/- about 1039 double 64-bit Double-precision floating point +/- about 10317 (Other types) char 16-bit A single character 18 boolean 1-bit A Boolean value (true or false) JAVA DATA TYPES - Abstract ▪ Abstract data types are based on primitive data types and have more functionality than primitive data types. For example, String is an abstract data type that can store letters, digits and other characters like / , () : ; $ and #. ▪ You cannot perform calculations on a variable of the string data type even if the data stored in it has digits. However, String provides methods for concatenating two strings, searching for one string within another, and extracting a portion of a string. The primitive data types do not have these features. 19 JAVA OPERATORS ▪ Arithmetic operators ▪ Assignment operators ▪ Unary operators ▪ Comparison operators ▪ Logical operators ▪ Conditional operators ▪ The new operator 20 JAVA OPERATORS - Arithmetic Operator Description Example Explanation + Addition c=a+b Adds the value of a and b and stores the result in c. - Subtraction c=a-b Subtracts b from a and stores the result in c * Multiplication c=a*b Multiplies the values a and b and stores the result in c. / Division c=a/b Divides a by b and stores the result in c. % Modulo c=a%b Divides a by b and stores the remainder in c. 21 JAVA OPERATORS - Assignment Operator Description Example Explanation = Assigns the value of the right a = b Assigns the value of b to a (b could be operand to the left an expression) += Adds the operands and assigns the a += b Adds the value of b to a (the result to the left operand expression could be written as a = a + b) -= Subtracts the right operand from a -= b Subtracts b from a the left operand and stores the a=a-b result in the left operand *= Multiplies the left operand by the a *= b Multiplies the values a and b and right operand and stores the result stores the result in a. a = a * b in the left operand /= Divides the left operand by the a /= b Divides a by b and stores the result in right operand and stores the result a. a = a / b in the left operand %= Divides the left operand by the a %= b Divides a by b and stores the right operand and stores the remainder in a. a = a % b remainder in the left operand 22 JAVA OPERATORS - Unary Operator Description Example Explanation ++ Increases the value of the operand a++ Equal to a = a+1 by one -- Decreases the value of the operand a-- Equal to a = a-1 by one The increment and decrement operator can be used in two ways: As a prefix, in which the operator precedes the variable: ++a; or --a; As a postfix, in which the operator follows the variable: a++; or a--; If the operator is used as a prefix, the value of the expression is modified before the assignment. When the operator is used as a postfix, the increment or decrement occurs only after the assignment. Therefore, when you say b = a++, the original value of a is assigned to b. When you say b = ++a, the incremented value of a is assigned to b. 23 JAVA OPERATORS - Comparison Operator Description Example Explanation == Evaluates whether the a == b Returns true if the values are operands are equal equal and false if otherwise != Evaluates whether the a != b Returns true if the values operands are not equal are not equal and false if otherwise > Evaluates whether the left a>b Returns true if a is greater operand is greater than the than b and false if otherwise right operand < Evaluates whether the left a= Evaluates whether the left a >= b Returns true if a is greater operand is greater than or than or equal to b and equal to the right operand false if otherwise 24 JAVA OPERATORS - Logical Operator Description Example Explanation && Evaluates to true if both a > 5 && The result is true if condition1 the conditions evaluate b < 10 and condition2 are both true, to true, false if otherwise if one of them is false, the result is false. || Evaluates to true if at a > 5 || The result is true if either least one of the b < 10 condition1 or condition2 or conditions evaluate both evaluate to true, if both to true, false if none of the conditions are false, the the conditions evaluates result is false. to true 25 JAVA OPERATORS - Conditional Operator Description Example Explanation (condition) Evaluates to true_val if a = (b > c) a is assigned the value of ? the condition returns ?b:c b if b is greater than c, true_val : true and false_val if the else a is assigned the false_val condition returns false value of c 26 JAVA OPERATORS - New When you create an instance of a class, you need to allocate memory for it. When you declare an object, you are simply stating its data type. For example, Person prof; This tells the compiler that the variable prof is an object of the Person class. It does not allocate memory for the object. To allocate memory, you need to use the new operator. Syntax: ; = new (); or = new (); 27 Order of Precedence [ ], () highest precedence ++, -- *, /, % +, - ==, != &&, || ?: =, +=, -=, *=, /=, %= ▪ 28 References: ▪ Malik, D.S. (2014). Java programming: problem analysis to program design. Pasig City: Cengage Learning Asia Pte Ltd. ▪ Farrell, J. (2016). Java programming. Australia: Cengage Learning. ▪ https://www.codemio.com/2016/06/the-top-7-free- ides-for-java.html (2016) ▪ Java™ Programming Language SL-275 by Suns Microsystems ▪ Oracle Java Documentation https://docs.oracle.com/javase/tutorial/java/nutsandb olts/_keywords.html Credits: ▪ Presentation template by SlidesCarnival 29 ▪ Photographs by Unsplash THANKS! Any questions? 30