Download - Java Syntax and DataType
Java syntax and Data type
Member: Leang Bunrong
Chea Socheat
Huon Sreynav
Heak Menghok
Bouy Sodeth
Introduction
• Variable Declaration and Naming Rules• Comments and Java Member• Primitive Data Type and Wrapper Class• Promotion and Casting• Operator
Contents
Variable Declaration
ប្រ�ភេ�ទនៃ� Variable• Instance variables (non-static fields):– No ‘static’ keyword– Value are unique to each instance of class– In-class lifetime
• Class variables (static fields)– Outside methods– Has ‘static’ keyword– Can be access by ClassName.VariableName– Usually declared as Constant– In-class or all-class accessible
http://www.tutorialspoint.com/java/java_variable_types.htm
Variable Declaration (cont.)
• Local Variables– are declared in methods, constructors, or blocks– No access modifier– Can be used in side the method or block only
• Parameters– No access modifier– Variables of Method for passing Value– Value assigned to Parameter call argument
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html
Naming Rules• Variable is case-sensitive• Can begin with letter (recommended), dollar sign ($),
underscore ( _ )• Can contain letters, numbers, underscores and dollar
sign(not recommended)• Can not contain other special characters, white space,
keywords, • ($) may found in some auto-generated names.• Should start with lowercase and with UPPERCASE for
the first character of the next word (e.g. helloWorld)
Java Fundamental Tutorial
• What is a comment? Why do we use a comment?
• We use comment for describe our code • We use comment for notice our code• We use comment for remember the point for
the other programmer when they look our code
Comments
• There are three types of comments– Comment line ( // )• When we want to comment only one line
– Comment multiline (/**..*/) (for documentation)• When we want to describe about documentation
– Comment multiline (/*….*/) (for text)• We use when we want to describe about the function of
our code or something else that u want to note for a multiline
Comments
Testing directly with eclipse by code
• Java member, it refer to Class member• Class member divide into two– Variable member–Method member
1. Variable member, it is the variable that declare with the static keyword. If it is not declare with the static keyword, so it is the instance variable
Java Member
2. Method member, it is a method that declare with the static keyword. If It is not declare with the static keyword, so it is the instance method.
• And both of them, if it is the class member it can work with the class directly by do not need to create a object and it can use with class directly and object class.
• Both of them if it is the instance, it can use with the class object only, it can not use with the class direct.
Java Member
• Primitive Data Type define the data type to variable
• Primitive Data Type is not a object• Primitive Data Type is a variable• Primitive Data Type has 8 Data Types
Primitive Data Type
http://www.javacamp.org/javaI/primitiveTypes.html#top & Java Fundamental Tutorial
Primitive Data Type
http://www.javacamp.org/javaI/primitiveTypes.html#top
• It is the class for convert from variable type to object type
• Especially, it always use with the Primitive Data Type
• and it has 8 Classes also• Primitive Data Type start with the lowercase
and Wrapper Classes start with the uppercase• And it can use for unwrap from object to
variable also by using method .XXXValue()
Wrapper Classes
http://way2java.com/java-lang/wrapper-classes/
Wrapper Classes
http://way2java.com/java-lang/wrapper-classes/
PromotionJava defines several type promotion rules that apply to expressions.
Here are the Type Promotion Rules:1.All byte and short values are promoted to int.2.If one operand is a long, the whole expression is promoted to long.
3.If one operand is a float, the entire expression is promoted to float.4.If any of the operands is double, the result is double.
public class Main {public static void main(String args[]) { byte b = 4;
float f = 5.5f; float result = (f * b); System.out.println("f * b = " + result);// f*b=22.0
} }
http://www.java2s.com/Book/Java/0020__Language-Basics/The_Type_Promotion_Rules.htm
Promotion
Type casting is the process of "casting" a value of a certain data type, into a storage variable that was designed to store a different data type. Casting here mean the conversion of that value to another version that could fit the variable of the different data type.There are way of Type Casting• Type Casting Primitive Data Typed
- Type casting an int to double - double to intExample: public class TypeCasting1 {
public static void main(String[]args){int x = 13;double y = (double)x; System.out.println("value of x : "+x); // 13System.out.println("value of y : "+y); // 13.0
} }
Casting
http://voidexception.weebly.com/type-casting-in-java-ii---type-casting-objects.html
Casting- Type casting integers to integers and floating points
to floating pointsexample:public class TypeCasting3 {
public static void main(String[]args){short x = 129;byte y = (byte)x; //need to type
cast here. size of x =16-bit > size of y = 8-bit
System.out.println("value of x = "+x);System.out.println("value of y = "+y);
}}
http://voidexception.weebly.com/type-casting-in-java-ii---type-casting-objects.html
Operator
Operator Result
= Simple assignment+= Addition assignment–= Subtraction assignment*= Multiplication assignment/= Division assignment%= Modulus assignment&= AND assignment^= XOR assignment|= OR assignment<<= Shift left assignment>>= Shift right assignment>>>= Shift right zero fill assignment
1. Assignment Operators
Operator Result + Addition– Subtraction (also unary minus)* Multiplication/ Division% Modulus
2. Arithmetic Operators
Operator
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html
3. Unary Operators
Operator Result + Unary plus Operator– Unary minus Operator++ Increment Operator– – Decrement Operator! Logical Complement Operator
Operator
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html
4. Equality and Relational Operators
Operator Result == Equal to!= Not equal to> Greater than>= Greater than or equal to< Less than<= Less than or equal to
Operator
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html
5. Condition Operators
Operator Result && Conditional-AND|| Conditional-OR?: Ternary Operator
Operator
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html
6. Type Comparison Operators
Operator Result Instanceof Compares an object to a specify type
Operator
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html
7. Bitwise and Bit Shift Operators
Operator Result ~ Unary bitwise complement<< Signed left shift>> Signed right shift>>> Unsigned right shift& Bitwise AND^ Bitwise exclusive OR| Bitwise inclusive OR
Operator
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html
Thank you for your pay attention