Lecture Java programming language: Variables and constants - Ho Dac Hung
Số trang: 15
Loại file: pdf
Dung lượng: 157.59 KB
Lượt xem: 5
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Lecture Java Programming Language: Variables and Constants provide knowledge about declaring variables, using variables, primitive data types, abstract data types, java packages, obtaining a value from the user, type casting, formatting numeric ouput, assignment operators, using named constants.
Nội dung trích xuất từ tài liệu:
Lecture Java programming language: Variables and constants - Ho Dac Hung Variables and Constants Ho Dac Hung1 Declaring Variables A variable is a name for a value stored in memory. Variables are used in programs so that values can be represented with meaningful names. A variable must be declared before it is used. A declaration takes the form: 2 Using Variables The value of a variable is changed throught assignment. An assignment statement is formed with the variable name on the left side of an equal sign and the value it is to receive on the right side of the equal sign. An assignment statement can be part of a variable declaration. In addition to being declared, the variable is initialized.3 Primitive Data Types byte short int long float double char boolean4 Abstract Data Types In addition to primitive data types, a variables can be declared using an abstract data type. One kind of abstract data type is the class. A variables declared with a class is called an object. The variable itself actually stores a reference to the area in memory where the object’s data and methods are stored.5 Java Packages Java SE includes numerous packages as part of the JDK. End-users will have the packages as part of the J2SE Runtime Enviroment (JRE). Packages follow a certain naming convention. Java packages start with java followed by a dot and than the package name. Companies and other organizations will often name a package with the organization name followed by a dot and then the package name.6 Java Packages The import statement is used to make the members of a package accessible to an application. To make a single class from a package accessible, a statement similar to the following is used: import java.util.Scanner; If several classes in a package are to be accessible, import statement, then the statement that imports the entire package may be used: import java.util.*;7 Obtaining a Value from the User An application is more flexible when values can be read from an input stream. Input stream is the sequence of characrers received from an input device, such as keyboard. To process data in the input stream, Java includes the Scanner class with methods for reading integers, floating point numbers, and strings.8 Type Casting Type casting converts a number (other types) of one type to a number of a different, but compatible type. Java will implicit type cast operands in a mixed expression to match the precision of the variable storing the result.9 Formatting Numeric Ouput The NumberFormat class, which is part ot java.text package, is used to create objects that format numbers. NumberFormat objects return a string that contains a formatted number. getCurrencyInstance(); getIntegerInstance(); getNumberInstance(); getPercentInstance();10 Assignment Operators In an assignment statement, the expression on the right side of the equal sign is evaluated first and then that value is given to the variable on the left. Because the expression on the right is evaluated before an assignment is made, it is possible to use the current value of the variable in the expression itself.11 Assignment Operators += -= *= /= %= …12 Using Named Constants A constant is a name for a memory location that stores a value that cannot be changed from its initial assignment. Constants, like variables, are used in programs so that values can be represented with meaningful names. A constant declaration is a variable declared final and takes the form: final = 13 Identifiers and Keywords Identifiers in Java must begin with a letter and may contain letters, numbers, and some special symbols. Periods and spaces are not allowed. Identifiers are also case sensitive, which mean that an uppercase letter is different from the same letter in lowercase.14 Identifiers and Keywords Java language contains keywords, which have special meaning to the Java compiler and therefore cannot be used for a variable or constant identifier.15
Nội dung trích xuất từ tài liệu:
Lecture Java programming language: Variables and constants - Ho Dac Hung Variables and Constants Ho Dac Hung1 Declaring Variables A variable is a name for a value stored in memory. Variables are used in programs so that values can be represented with meaningful names. A variable must be declared before it is used. A declaration takes the form: 2 Using Variables The value of a variable is changed throught assignment. An assignment statement is formed with the variable name on the left side of an equal sign and the value it is to receive on the right side of the equal sign. An assignment statement can be part of a variable declaration. In addition to being declared, the variable is initialized.3 Primitive Data Types byte short int long float double char boolean4 Abstract Data Types In addition to primitive data types, a variables can be declared using an abstract data type. One kind of abstract data type is the class. A variables declared with a class is called an object. The variable itself actually stores a reference to the area in memory where the object’s data and methods are stored.5 Java Packages Java SE includes numerous packages as part of the JDK. End-users will have the packages as part of the J2SE Runtime Enviroment (JRE). Packages follow a certain naming convention. Java packages start with java followed by a dot and than the package name. Companies and other organizations will often name a package with the organization name followed by a dot and then the package name.6 Java Packages The import statement is used to make the members of a package accessible to an application. To make a single class from a package accessible, a statement similar to the following is used: import java.util.Scanner; If several classes in a package are to be accessible, import statement, then the statement that imports the entire package may be used: import java.util.*;7 Obtaining a Value from the User An application is more flexible when values can be read from an input stream. Input stream is the sequence of characrers received from an input device, such as keyboard. To process data in the input stream, Java includes the Scanner class with methods for reading integers, floating point numbers, and strings.8 Type Casting Type casting converts a number (other types) of one type to a number of a different, but compatible type. Java will implicit type cast operands in a mixed expression to match the precision of the variable storing the result.9 Formatting Numeric Ouput The NumberFormat class, which is part ot java.text package, is used to create objects that format numbers. NumberFormat objects return a string that contains a formatted number. getCurrencyInstance(); getIntegerInstance(); getNumberInstance(); getPercentInstance();10 Assignment Operators In an assignment statement, the expression on the right side of the equal sign is evaluated first and then that value is given to the variable on the left. Because the expression on the right is evaluated before an assignment is made, it is possible to use the current value of the variable in the expression itself.11 Assignment Operators += -= *= /= %= …12 Using Named Constants A constant is a name for a memory location that stores a value that cannot be changed from its initial assignment. Constants, like variables, are used in programs so that values can be represented with meaningful names. A constant declaration is a variable declared final and takes the form: final = 13 Identifiers and Keywords Identifiers in Java must begin with a letter and may contain letters, numbers, and some special symbols. Periods and spaces are not allowed. Identifiers are also case sensitive, which mean that an uppercase letter is different from the same letter in lowercase.14 Identifiers and Keywords Java language contains keywords, which have special meaning to the Java compiler and therefore cannot be used for a variable or constant identifier.15
Tìm kiếm theo từ khóa liên quan:
Java programming language Bài giảng ngôn ngữ lập trình Java Primitive data types Abstract data types Formatting numeric ouput Using named constantsGợi ý tài liệu liên quan:
-
Bài giảng Chương 1: Cơ bản về ngôn ngữ lập trình Java
19 trang 26 0 0 -
Bài giảng Ngôn ngữ lập trình Java: Chương 6.2 - TS. Phan Nguyên Hải
34 trang 25 0 0 -
Bài giảng Ngôn ngữ lập trình Java: Phần 2 - TS. Vũ Hữu Tiến
65 trang 24 0 0 -
Ebook Teach yourself data structures and algorithms in 24 hours
548 trang 23 0 0 -
Lecture Introduction to Java programming - Chapter 15: Graphics
32 trang 21 0 0 -
Bài giảng Ngôn ngữ lập trình Java căn bản
115 trang 21 0 0 -
Bài giảng Công nghệ Java: Chương 0 - Trần Quang Diệu
6 trang 21 0 0 -
Lecture Introduction to Java programming - Chapter 1: Introduction to computers, programs, and Java
63 trang 18 0 0 -
Bài giảng Ngôn ngữ lập trình Java: Chương 4 - TS. Phan Nguyên Hải
56 trang 18 0 0 -
Lecture Java programming language: Inheritance and Polymorphism - Ho Dac Hung
11 trang 17 0 0