Download presentation
Presentation is loading. Please wait.
Published byOsborn Howard Modified over 8 years ago
1
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม Email: ratapoom@orjix.com
2
Object-Oriented Programming What is an Object? An object is a software bundle of related state and behavior Software objects are often used to model the real-world objects that you find in everyday life. Dogs have state (name, color, breed) and behavior (barking, wagging tail) An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods (functions in some programming languages). Data Encapsulation- Hiding internal state and requiring all interaction to be performed through an object's methods current speed, current pedal cadence, and current gear
3
Object-Oriented Programming What is a Class? A class is a blueprint or prototype from which objects are created. A class models the state and behavior of a real-world object Your bicycle is an instance of the class of objects known as bicycles.
4
Object-Oriented Programming What is a Class?
5
Language Basics Variables Instant Variables (Non-Static Fields) objects store their individual states in "non-static fields“ Non-static fields are also known as instance variables because their values are unique to each instance of a class (to each object, in other words) Class Variables (Static Fields) A class variable is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated Local Variables local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class. Parameters public static void main(String[] args)
6
Language Basics Naming Variable names are case-sensitive. When choosing a name for your variables, use full words instead of cryptic abbreviations. the name you choose must not be a keyword or reserved word If the name you choose consists of only one word, spell that word in all lowercase letters. If it consists of more than one word, capitalize the first letter of each subsequent word. f your variable stores a constant value, such as static final int NUM_GEARS = 6, the convention changes slightly, capitalizing every letter and separating subsequent words with the underscore character.
7
Language Basics Primitive Data Types byte : The byte data type is an 8-bit signed two's complement integer. short : The short data type is a 16-bit signed two's complement integer. int : The int data type is a 32-bit signed two's complement integer. long : The long data type is a 64-bit signed two's complement integer. float : The float data type is a single-precision 32-bit IEEE 754 floating point. double : The double data type is a double- precision 64-bit IEEE 754 floating point. boolean : The boolean data type has only two possible values: true and false. char : The char data type is a single 16-bit Unicode character. Data Type Default Value (for fields) byte0 short0 int0 long0L float0.0f double0.0d char'\u0000' String (or any object) null booleanfalse Fields that are declared but not initialized will be set to a reasonable default by the compiler. Local variables are slightly different; the compiler never assigns a default value to an uninitialized local variable.
8
Language Basics Literal A literal is the source code representation of a fixed value; literals are represented directly in your code without requiring computation. The integral types (byte, short, int, and long) can be expressed using decimal, octal, or hexadecimal number systems. Use 'single quotes' for char literals and "double quotes" for String literals. \b (backspace), \t (tab), \n (line feed), \f (form feed), \r (carriage return), \" (double quote), \' (single quote), and \\ (backslash).
9
Language Basics Arrays An array is a container object that holds a fixed number of values of a single type. Each item in an array is called an element Each element is accessed by its numerical index. an array declaration has two components: the array's type and the array's name. An array's type is written as type[], where type is the data type of the contained elements; the square brackets are special symbols indicating that this variable holds an array.
10
Language Basics Creating, Initializing, and Accessing an Array
11
Language Basics Copying Arrays and Multidimensional Arrays a multidimensional array is simply an array whose components are themselves arrays
12
Language Basics Assignment, Arithmetic, and Unary Operators The Simple Assignment Operator The Arithmetic Operators
13
Language Basics Assignment, Arithmetic, and Unary Operators The Unary Operator
14
Language Basics Equality, Relational, and Conditional Operators The Equality and Relational Operators
15
Language Basics Equality, Relational, and Conditional Operators The Conditional Operators
16
Language Basics Equality, Relational, and Conditional Operators The Type Comparison Operator instanceof
17
Language Basics Expresssions, Statements, and Blocks Operators may be used in building expressions, which compute values; expressions are the core components of statements; statements may be grouped into blocks. An expression is a construct made up of variables, operators, and method invocations, which are constructed according to the syntax of the language, that evaluates to a single value.
18
Language Basics Expresssions, Statements, and Blocks Statements are roughly equivalent to sentences in natural languages. A statement forms a complete unit of execution. The following types of expressions can be made into a statement by terminating the expression with a semicolon (;). Assignment expressions Any use of ++ or -- Method invocations Object creation expressions
19
Language Basics Expresssions, Statements, and Blocks A block is a group of zero or more statements between balanced braces and can be used anywhere a single statement is allowed
20
Language Basics Control Flow Statements The statements inside your source files are generally executed from top to bottom, in the order that they appear. Control flow statements, however, break up the flow of execution by employing decision making, looping, and branching, enabling your program to conditionally execute particular blocks of code. the decision-making statements (if-then, if-then-else, switch) the looping statements (for, while, do-while) the branching statements (break, continue, return)
21
Language Basics The if-then and if-then-else Statements The if-then Statement The if-then-else Statement
22
Language Basics The switch Statement A switch works with the byte, short, char, and int primitive data types. An if-then-else statement can be used to make decisions based on ranges of values or conditions, whereas a switch statement can make decisions based only on a single integer or enumerated value.
23
Language Basics The switch Statement
24
Language Basics The while and do-while Statements The while Statement The do-while Statement
25
Language Basics The for Statements The initialization expression initializes the loop; it's executed once, as the loop begins. When the termination expression evaluates to false, the loop terminates. The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value.
26
Language Basics Branching Statements The break Statement (unlabeled/labeled)
27
Language Basics Branching Statements The continue Statement (unlabeled/labeled)
28
References http://java.sun.com/docs/books/tutorial/essentia l/index.html http://java.sun.com/ http://www.eclipse.org/ http://java.sun.com/docs/codeconv/html/Code ConvTOC.doc.html
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.