26
Java syntax and Data type

Java Syntax and DataType

Embed Size (px)

DESCRIPTION

Understanding: - Java datatype and scope - Variable Declaration - Naming Rules - Primitive Datatype and Wrapper class - Promotion and Casting - Operator

Citation preview

Page 1: Java Syntax and DataType

Java syntax and Data type

Page 2: Java Syntax and DataType

Member: Leang Bunrong

Chea Socheat

Huon Sreynav

Heak Menghok

Bouy Sodeth

Introduction

Page 3: Java Syntax and DataType

• Variable Declaration and Naming Rules• Comments and Java Member• Primitive Data Type and Wrapper Class• Promotion and Casting• Operator

Contents

Page 4: Java Syntax and DataType

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

Page 5: Java Syntax and DataType

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

Page 6: Java Syntax and DataType

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

Page 7: Java Syntax and DataType

• 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

Page 8: Java Syntax and DataType

• 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

Page 9: Java Syntax and DataType

• 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

Page 10: Java Syntax and DataType

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

Page 11: Java Syntax and DataType

• 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

Page 12: Java Syntax and DataType

Primitive Data Type

http://www.javacamp.org/javaI/primitiveTypes.html#top

Page 13: Java Syntax and DataType

• 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/

Page 14: Java Syntax and DataType

Wrapper Classes

http://way2java.com/java-lang/wrapper-classes/

Page 15: Java Syntax and DataType

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

Page 16: Java Syntax and DataType

Promotion

Page 17: Java Syntax and DataType

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

Page 18: Java Syntax and DataType

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

Page 19: Java Syntax and DataType

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

Page 20: Java Syntax and DataType

Operator Result + Addition– Subtraction (also unary minus)* Multiplication/ Division% Modulus

2. Arithmetic Operators

Operator

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html

Page 21: Java Syntax and DataType

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

Page 22: Java Syntax and DataType

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

Page 23: Java Syntax and DataType

5. Condition Operators

Operator Result && Conditional-AND|| Conditional-OR?: Ternary Operator

Operator

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html

Page 24: Java Syntax and DataType

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

Page 25: Java Syntax and DataType

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

Page 26: Java Syntax and DataType

Thank you for your pay attention