0% found this document useful (0 votes)
4 views22 pages

01 Java Basics

The document provides an introduction to Java programming, covering basic concepts such as class structure, naming conventions, comments, and variable types. It explains the differences between local, instance, and static variables, as well as the importance of the main method as the entry point of Java applications. Additionally, it briefly discusses data structures like stacks and their operations.

Uploaded by

panpepe950120
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views22 pages

01 Java Basics

The document provides an introduction to Java programming, covering basic concepts such as class structure, naming conventions, comments, and variable types. It explains the differences between local, instance, and static variables, as well as the importance of the main method as the entry point of Java applications. Additionally, it briefly discusses data structures like stacks and their operations.

Uploaded by

panpepe950120
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Object-Oriented Programming

Class 1: Java Basics


人工智慧研究中心
逢甲大學 資 訊 工 程 學 系

許懷中

1
Your First Java Program
// your first java program
// FileName : "[Link]"

public class HelloWorld


{
/*
Your program begins with a call to main().
main function is the entry point of your system
this java program prints "Hello, World"
to the terminal window.
*/

public static void main(String args[])


{
[Link]("Hello, World");
}
}
Java Conventions
▪ Class names: In Java, the first letter of class name should be
uppercase.
▪ Course, HelloWorld, etc.
▪ Method name: All method names in Java start with a lowercase letter.
If the method name comprises more than one word, then the first
letter of each of these inner words will be uppercase.
▪ add(), addToTrackingList()
▪ Program file name: The filename of the Java program should be the
same as the name of the public class with an extension “.java”.
▪ [Link]
▪ You can have your name for the source file if no public class in the source.
▪ Case sensitive: Java programming language is case-sensitive.
▪ [Link]("Hello, World");
▪ [Link]("Hello, World");
▪ Main method: The method ‘main’ is the starting point of execution
and is a compulsory method in all Java programs.
Comments in Java
▪ Single line comments
// your first java program
// FileName : "[Link]"
▪ Multiple lines comments
/*
Your program begins with a call to main().
main function is the entry point of your system
this java program prints "Hello, World"
to the terminal window.
*/
Method Names
▪ All the method names should start with a lowercase
letter.
▪ If several words are used to form the name of the
method, then each first letter of the inner word should
be in Uppercase. Underscores are allowed, but not
recommended. Also allowed are digits and currency
symbols.
▪ Example
▪ public void doSomething() // valid syntax
▪ public void DoSomething() // valid syntax, but discouraged
▪ public void do_something() // valid syntax, but discouraged
▪ public static void doSomething() //valid, different usage
Identifiers
▪ Identifiers are the names of local variables, instance and class
variables, and labels, but also the names for classes, packages,
modules and methods. All Unicode characters are valid.
▪ All identifiers can begin with a letter, a currency symbol or an
underscore (_). According to the convention, a letter should be
lower case for variables.
▪ The first character of identifiers can be followed by any
combination of letters, digits, currency symbols and the
underscore. The underscore is not recommended for the names
of variables.
▪ Constants (static final attributes and enums) should be in all
Uppercase letters.
▪ Most importantly identifiers are case-sensitive.
▪ A keyword cannot be used as an identifier since it is a reserved
word and has some special meaning.
Keywords
Variables
▪ Declaration
int count;
int count = 0;
int[] arr = new int[3];
// each element of the array is initialised to 0
int[] arr = {1, 2, 3};
int[] arr = new int[]{1, 2, 3};
Variables (cont.)
▪ Local Variables: A variable defined within a
block or method or constructor is called a local
variable.
▪ These variables are created when the block is
entered, or the function is called and destroyed
after exiting from the block or when the call returns
from the function.
▪ The scope of these variables exists only within the
block in which the variables are declared, i.e., we
can access these variables only within that block.
▪ Initialization of the local variable is mandatory
before using it in the defined scope.
Variables (cont.)
▪ Instance Variable: Instance variables are non-static
variables and are declared in a class outside of any
method, constructor, or block.
▪ As instance variables are declared in a class, these
variables are created when an object of the class is
created and destroyed when the object is destroyed.
▪ Unlike local variables, we may use access specifiers for
instance variables. If we do not specify any access
specifier, then the default access specifier will be used.
▪ Initialization of an instance variable is not mandatory.
Its default value is 0.
▪ Instance variables can be accessed only by creating
objects.
Variables (cont.)
▪ Static Variable: also known as class variable.
▪ These variables are declared similarly as instance variables. The
difference is that static variables are declared using the static
keyword within a class outside of any method, constructor or block.
▪ Unlike instance variables, we can only have one copy of a static
variable per class, irrespective of how many objects we create.
▪ Static variables are created at the start of program execution and
destroyed automatically when execution ends.
▪ Initialization of a static variable is not mandatory. Its default value
is 0.
▪ If we access a static variable like an instance variable (through an
object), the compiler will show a warning message, which won’t
halt the program. The compiler will replace the object name with
the class name automatically.
▪ If we access a static variable without the class name, the compiler
will automatically append the class name.
Data Types
Primitive Data Types
Operators
Decision Making
Loops
How JAVA Works - JVM
Learning OOP using
Data Structures
▪ Stacks
▪ Queue
Stacks (堆疊)
top F
top E E
D D
C C
B B
bottom A bottom A

▪ 加入新盤子時,須從堆疊頂端 (top) 加入
▪ Adding a new item onto the top of a stack
▪ 移去時亦如此
▪ Removing an item from the top of a stack
▪ 堆疊是一個 FILO (後進先出) 的 list
▪ First in last out (FILO) style list
Stacks (堆疊)
▪ Standard operations:
▪ IsEmpty: return true iff stack is empty
▪ IsFull: return true iff stack has no remaining
capacity
▪ Top: return top element of stack
▪ Push: add an element to the top of the stack
▪ Pop: delete the top element of the stack
Stacks
▪ 以 1D array 來實作 stack
▪ Implementing a stack using 1D array
▪ Stack elements 儲存於 stack[0] 到 stack[top]
之間
▪ Storing the elements of a stacks between
stack[0] to stack[top]
Stacks
a b c d e
0 1 2 3 4 5 6
▪ stack top is at element e (top == 4)
▪ IsEmpty() => check whether top < 0
▪ IsFull() => check whether top ==
capacity – 1
▪ Top() => If not empty return stack[top]
▪ Push(theElement) => if full then raise alert
otherwise add theElement at stack[top+1]
▪ Pop() => if empty then raise alert, otherwise
delete stack[top] from the stack

You might also like