Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

2 Java Basic Syntax

The document provides an overview of the Java programming language, including its history, syntax, semantics, and structure. It explains the components of a Java program, such as classes and methods, and discusses key concepts like variable declaration, strong typing, and coding standards. Additionally, it highlights the importance of following naming conventions and best practices in Java programming.

Uploaded by

darienvalmeo08
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

2 Java Basic Syntax

The document provides an overview of the Java programming language, including its history, syntax, semantics, and structure. It explains the components of a Java program, such as classes and methods, and discusses key concepts like variable declaration, strong typing, and coding standards. Additionally, it highlights the importance of following naming conventions and best practices in Java programming.

Uploaded by

darienvalmeo08
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Write Once, Compile, and Run Anywhere

ROSALYN P. REYES, MSIT


The Java Language

• Created by Sun Microsystems


• Introduced in 1995, initial popularity grew due
to Internet applications
• Excitement surrounding Java applets
• Confusion with Javascript
• Steady rise in popularity has continued for
“better” programming reasons JAMES GOSLING
“The father of Java”

2
Syntax and Semantics
• The syntax rules of a language define how we can combine reserved
words, symbols, and identifiers
• The semantics of a program statement define what the statement
means
• Problem with program syntax = “error”
• Problem with program semantics = “bug”

3
The Java Language (cont’d)
• … is a high-level programming language
• … is very object oriented
• … is similar to C++ and C
• … typically compiled to Java bytecode
• … is often confused with the Java Platform, but these are two
different aspects of “Java”

4
Java Program Structure
• A Java program consists of:
• One or more classes
• A class contains one or more methods
• A method contains program statements
• We will explore these terms in detail

5
Java Program Structure
// comments about the class
public class ClassName
{
class h ead er
-the first line of code
in a Java class and it
defines the name of the
class b o d y class.
consists of the keyword "
class " and the name you give
to the class.

C o m m en ts can b e p laced alm o st an yw h ere


}

6
Java Program Structure
-m eth o d h ead er
// comments about the class
-d river co d e
public class ClassName
{

public static void main (String[] args)


//java program processing starts from the
/*main() method which is mandatory part
of every java program.*/
{

m eth o d b o d y

}
7
Hello World

// HelloWorld.java
public class HelloWorld
{
public static void main (String[] args)
{
System.out.println(“Hello World!”);
}
}

8
Hello World
// HelloWorld.java
public class HelloWorld
{
public static void main (String[] args)
{
System.out.println(“Hello World!”);
}
}

• Creates a “class” called HelloWorld


-Compiled to HelloWorld.class
-Classes used to define objects

9
Hello World
// HelloWorld.java
public class HelloWorld
{
public static void main (String[] args)
{
System.out.println(“Hello World!”);
}
}

• The “main” method is where it starts to run


• Ignore “public static void” and “String[]
args” for now

10
Hello World
// HelloWorld.java
public class HelloWorld
{
public static void main (String[] args)
{
System.out.println(“Hello World!”);
}
}

• Contains one “statement”


• The System.out.println function comes from the
Java “class library”
• Ends with a semicolon (all statements do)

11
Compiling and Running
• Create the file HelloWorld.java in a text editor
• Compile:
• javac HelloWorld.java
• Run:
• java HelloWorld
• Output:
• Hello World!

12
Comments
• Three kinds of comments:
// a one-line comment
/* a multi-line
comment */
/** a javadoc comment */

• To simplify: comments are good

13
Statements & Blocks
• A simple statement is a command terminated by a semi-colon:

• A block is a compound statement enclosed in curly brackets:


public class MainClass {
public static void main(String[] args) {
String s1 = "1 " + "2";
String s2 = s1 + " = 3";
System.out.println(s1);
System.out.println(s2);}}
• Blocks may contain other blocks

BULSU CICT 14
Reserved Words and Identifiers
• Reserved words
• are specified by the language
• All Java reserved words are in the text
• Identifiers
• are specified by a programmer
• Maybe you: e.g. HelloWorld
• Maybe someone else: e.g. println

15
P a s c a lC a s e (U p p e r
Restrictions and Conventions C a m e l C a s e ):
In PascalCase, each word in the identifier
is capitalized, including the first one. It’s
often used in programming languages for
• Restriction naming classes, records, and other user-
• Identifiers can not start with a digit defined types. Ex:
CustomerService, UserProfile,
• Conventions an d LoginAccounts
• PascalCase/Title case for class
names: HelloWorld
• Uppercase for constants: MAX camelCase (Lower Camel Case):
camelCase is similar to PascalCase, but the first letter of the
• CamelCase first word is in lowercase. It’s commonly used for naming
• Methods/functions, variables variables and functions.
Ex: customerService, userProfile , and
loginAccounts

16
Java Identifiers

• All java components require names. Names used for classes, variables and
methods are called
• In java there are several points to remember about identifiers. They are as
follows:
• All identifiers should begin with a letter ( ), currency character ($) or an
underscore (-).
• After the first character identifiers can have any combination of characters.
• A CANNOT be used as an identifier.
• Most importantly identifiers are

BULSU CICT 17
Identifiers

NewClass New Class


totalQty 1totalQty
unit_price first!
_getValues()
?variable1
sum_of_products setNew Value()
$totalAmt

BULSU CICT 18
Basic Syntax
• C a s e S e n s i t i v i t y - JAVA I S C A S E S E N S I T I V E w h i c h m e a n s
identifier Hello and hello would have different meaning in Java.
• Class Names - For all class names the first letter should be in Upper Case.

If several words are used to form a name of the class each inner words first
letter should be in Upper Case.

Example: class MyFirstJavaClass

BULSU CICT 19
White Space Conventions

• Idea: make programs easy to read


• Use consistent indentation
• Use blank lines and comments to visually separate methods
• The fact that it compiles DOESN’T MAKE IT RIGHT.

20
Strong Typing
• Java is a “strongly typed” language
• All variables and values have a specific type
• Type is known when the program is compiled…. before it is run
• So all variables and values must be declared with a type before being
used

21
Declaring Variables
The Java variable declaration creates a new variable with required properties. The
programming language requires four basic things to declare a variable in the program.

4
• Syntax:
Data_type variable_name = valu
e;
1 2 3

Examples:
int count1, int count 2;
int count = 0;
String studentName = “Raiqa
Reyes”;
char letterA = ‘A’;
22
Variables
• They are used to store data such as numbers and letters.

Example:
char letterA = ‘A’;
variable = expression or value;
double num1,num2;
String studentName;
boolean status;
status = true;
BULSU CICT 23
Assignment
• We use the = operator for variable assignment
• Initialization is a special case
• When a value is assigned, the old value is overwritten
• In Java, we use the final modifier to declare a variable constant
• final int MAX_HEIGHT = 6;

24
Coding Standards & Best Practices To Follow
1. Write as few lines as possible.
2. Use appropriate naming conventions.
3. Segment blocks of code in the same section into paragraphs.
4. Use indentation to marks the beginning and end of control structures.
Clearly specify the code between them.
5. Don’t use lengthy functions. Ideally, a single function should carry out
a single task.
6. Use the DRY (Don’t Repeat Yourself) principle. Automate repetitive tasks
whenever necessary. The same piece of code should not be repeated in the
script.
7. Avoid Deep Nesting. Too many nesting levels make code harder to read and
follow.
8. Capitalize special words and function names to distinguish them from
table and column names.
9. Avoid long lines. It is easier for humans to read blocks of lines that
are horizontally short and vertically long.
VALID OR NOT
_myvariable $variabletest B5
1Student
1$test_variable TestVariable
id_no_
a2 3x
!ValidResults
totalAmt_1 -averageGrade

10_discounted_price x1

You might also like