Syntax and Data Type
Content
1. Variable declaration
2. Java Naming Rule
3. Comments
4. Primitive Data
5. Type/Wrapper Class
6. Auto Boxing
7. Promotion and Casting
8. Operators
Variable Declaration
● Variable is a name of memory location.
● Variable Declaration:
[access_modifier] Datatype varName [ = value];
● There are three types of variables in Java:
○ Instance variable
○ Class Variable or Static variable
○ Local variable
Variable Declaration
● Instance variable (non-static fields):
○ Declared outside a method, constructor, block
○ Are visible to all methods, constructors and block in the class
○ No “static” keyword
○ Values are unique to instance of class
○ Has a default value
Variable Declaration
● Static or Class variable (static fields):
○ Must be declared outside of methods
○ Has “static” keyword
○ Can be accessed by ClassName.VariableName
○ Has a default value
Variable Declaration
● Local Variables
○ Declared in a method, constructor or block
○ No access modifier
○ Lifetime is only within declared block
○ Automatically destroyed once it exits the block
○ No default value => Must be initial a value before used
○ Parameters are also local variables:
■ No access modifier
■ Used for passing a value for method
Variable Declaration
Java 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, whitespace, keywords
● Should start lowercase and with UPPERCASE for the character of the next word (eg.
teacherName)
Comments
● Describe the definition of code
● Remark the code
● Documenting the code
● Get ignored by the compiler
● There are 3 types of comments:
○ Single line
○ Multi line
○ Documents
Comments
● Single line : // (Double forward slash)
Comments
● Multi-line : /* ................ */
Comments
● Documents: /**...............*/
Primitive Data Types / Wrapper Class
Primitive Data Types
● Java defines primitive types of data into 8 types: byte, short, int, long, char, float,
double, and boolean
● Primitive Data type is divided into 4 groups:
○ Integer: byte, short, int, long - that are used for whole-valued signed numbers
○ Floating-point numbers: float and double - that is used for decimal numbers
○ Character: char - is represented for symbols in character set such as letters and
numbers
○ Boolean: boolean – is a special data type that is represented true/false values.
Primitive Data Types / Wrapper Class
● Integers and Floating-point types
●
●
●
●
● Character
Primitive Data Types / Wrapper Class
● Character
○ In Java, char data type has a size of 16-bit that is range from ‘u0000’ to ‘uffff’ or
0-65535
● Boolean
○ boolean data type has 1-Bit size that stores values of 0, 1 represented of false and
true
Primitive Data Types / Wrapper Class
Wrapper Class
● provides the mechanism for converting primitive data types to Object Wrapper Class.
● For convert data type from String to primitive data type
● provide some methods for usage
Primitive Data Types / Wrapper Class
Wrapper Class
Auto Boxing
● Autoboxing is the automatic conversion that Java compiler makes between the
primitive data types and their corresponding object wrapper class
E.g Integer i = 9; // This is autoboxing
// Compiler will replace 9 with Integer.valueOf(9)
Unboxing
● Unboxing is the automatic conversion from object wrapper class to primitive data
type
E.g
Integer i = new Integer(9);
int j = i; // This is unboxing
// int j = i; is equal to int j = i.intValue();
Promotion & Casting
Promotion
● Promotion is an automatic type conversion
● No data losing
● No casting operator
Promotion & Casting
Casting
● Casting is type conversion to a specific data type.
● But there are some points that we can not cast such as:
○ Primitive type to Reference type
○ Null value to primitive type
○ Primitive type to Boolean
○ Boolean to other primitive types
Operators
● Operator in java is a symbol that is used to perform operations
● Here are some basic operators that are commonly used:
○ Assignment Operator
○ Arithmetic Operator
○ Relational / Comparison Operator - Bitwise Operator
○ Logical Operator
Operators
● Assignment Operators
Operators
● Arithmetic Operators
Operators
● Relational Operators
Operators
● Bitwise Operators
Operators
● Logical Operators
Thank you!

2._Java_Syntax_and_Data_Type.pptx.pdf

  • 1.
  • 2.
    Content 1. Variable declaration 2.Java Naming Rule 3. Comments 4. Primitive Data 5. Type/Wrapper Class 6. Auto Boxing 7. Promotion and Casting 8. Operators
  • 3.
    Variable Declaration ● Variableis a name of memory location. ● Variable Declaration: [access_modifier] Datatype varName [ = value]; ● There are three types of variables in Java: ○ Instance variable ○ Class Variable or Static variable ○ Local variable
  • 4.
    Variable Declaration ● Instancevariable (non-static fields): ○ Declared outside a method, constructor, block ○ Are visible to all methods, constructors and block in the class ○ No “static” keyword ○ Values are unique to instance of class ○ Has a default value
  • 5.
    Variable Declaration ● Staticor Class variable (static fields): ○ Must be declared outside of methods ○ Has “static” keyword ○ Can be accessed by ClassName.VariableName ○ Has a default value
  • 6.
    Variable Declaration ● LocalVariables ○ Declared in a method, constructor or block ○ No access modifier ○ Lifetime is only within declared block ○ Automatically destroyed once it exits the block ○ No default value => Must be initial a value before used ○ Parameters are also local variables: ■ No access modifier ■ Used for passing a value for method
  • 7.
  • 8.
    Java 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, whitespace, keywords ● Should start lowercase and with UPPERCASE for the character of the next word (eg. teacherName)
  • 9.
    Comments ● Describe thedefinition of code ● Remark the code ● Documenting the code ● Get ignored by the compiler ● There are 3 types of comments: ○ Single line ○ Multi line ○ Documents
  • 10.
    Comments ● Single line: // (Double forward slash)
  • 11.
    Comments ● Multi-line :/* ................ */
  • 12.
  • 13.
    Primitive Data Types/ Wrapper Class Primitive Data Types ● Java defines primitive types of data into 8 types: byte, short, int, long, char, float, double, and boolean ● Primitive Data type is divided into 4 groups: ○ Integer: byte, short, int, long - that are used for whole-valued signed numbers ○ Floating-point numbers: float and double - that is used for decimal numbers ○ Character: char - is represented for symbols in character set such as letters and numbers ○ Boolean: boolean – is a special data type that is represented true/false values.
  • 14.
    Primitive Data Types/ Wrapper Class ● Integers and Floating-point types ● ● ● ● ● Character
  • 15.
    Primitive Data Types/ Wrapper Class ● Character ○ In Java, char data type has a size of 16-bit that is range from ‘u0000’ to ‘uffff’ or 0-65535 ● Boolean ○ boolean data type has 1-Bit size that stores values of 0, 1 represented of false and true
  • 16.
    Primitive Data Types/ Wrapper Class Wrapper Class ● provides the mechanism for converting primitive data types to Object Wrapper Class. ● For convert data type from String to primitive data type ● provide some methods for usage
  • 17.
    Primitive Data Types/ Wrapper Class Wrapper Class
  • 18.
    Auto Boxing ● Autoboxingis the automatic conversion that Java compiler makes between the primitive data types and their corresponding object wrapper class E.g Integer i = 9; // This is autoboxing // Compiler will replace 9 with Integer.valueOf(9)
  • 19.
    Unboxing ● Unboxing isthe automatic conversion from object wrapper class to primitive data type E.g Integer i = new Integer(9); int j = i; // This is unboxing // int j = i; is equal to int j = i.intValue();
  • 20.
    Promotion & Casting Promotion ●Promotion is an automatic type conversion ● No data losing ● No casting operator
  • 21.
    Promotion & Casting Casting ●Casting is type conversion to a specific data type. ● But there are some points that we can not cast such as: ○ Primitive type to Reference type ○ Null value to primitive type ○ Primitive type to Boolean ○ Boolean to other primitive types
  • 22.
    Operators ● Operator injava is a symbol that is used to perform operations ● Here are some basic operators that are commonly used: ○ Assignment Operator ○ Arithmetic Operator ○ Relational / Comparison Operator - Bitwise Operator ○ Logical Operator
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.