0% found this document useful (0 votes)
4 views

lecture1

The document introduces Java as a programming language used in various applications like Minecraft and Netflix. It explains basic Java syntax, including the structure of a Java program, the importance of class and method definitions, and how to print output using System.out.println() and System.out.print(). Additionally, it covers concepts like comments, string literals, and provides a lab exercise for practice.

Uploaded by

ch.f14.mark
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

lecture1

The document introduces Java as a programming language used in various applications like Minecraft and Netflix. It explains basic Java syntax, including the structure of a Java program, the importance of class and method definitions, and how to print output using System.out.println() and System.out.print(). Additionally, it covers concepts like comments, string literals, and provides a lab exercise for practice.

Uploaded by

ch.f14.mark
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Unit 1: Primitive Types

Basic Java Syntax


Java

What do Minecraft, Android phones, and Netflix have in


common? They’re all programmed in Java!

Many of the apps you use in an Android phone or tablet are also
written in Java. Netflix uses Java for some of its software too.
Java is used worldwide to create software that we all use.

2
Java
Java is a programming language, which means that we can
use Java to tell a computer what to do.

Computers don’t actually speak Java so we have


to compile (translate) Java source files (they end in .java) into
class files (they end in .class).

The source file is something humans can read and edit, and the
class file is code that a computer can understand and can run.
3
Java Terminology
All Java code are organized into units called classes.

class:
(a) A module or program that can contain executable
code.
(b) A description of a type of objects. (Animal class,
Human class, Employee class, Car class)

statement: An executable piece of code that represents a


complete command to the computer.
– every basic Java statement ends with a semicolon ;

method: A named sequence of statements that can be executed


together to perform a particular action or computation. 4
Structure of a Java program
public class name { class: a program
public static void main(String[] args) {
statement;
statement; method: a named group
... of statements
statement;
}
} statement: a command to be executed

• Every executable Java program consists of a class, called the


driver class,
– that contains a method named main,
• that contains the statements (commands) to be executed.
5
Program Template
Here's a simple program that prints out a message on the screen. Code
highlighted in red should be in every program!

public class Main{


public static void main(String[] args){
System.out.println("Hello, World!");
}
}

Output:
Hello, World!

6
First Program: repl.it
We will use repl.it, an online integrated development
environment(IDE), for the first part of this course to write all of
our code.
Click on “run” to compile and
run your code!

console: Text box into which


the program's output is printed.

7
File naming
The name of the class has to match up with the name of the file.

For example, the class below is called Main therefore the name
of the file is Main.java. On replit, the main class must be called
Main.java.(in other IDEs, you can pick any name.)

8
Printing

Two ways to print a line of output on the console:


System.out.println() and System.out.print().

System.out.println() is just the way that you ask Java to


print out the value of something followed by a new line (ln).

System.out.print() without the ln will print out something


without advancing to the next new line.

9
System.out.println
public class Welcome{
public static void main(String[] args){
System.out.println("Hi there!");
System.out.println(”Welcome to APCS A!");
}
}
Output:
Hi There!
Welcome to APCS A!

The “System” in System.out.println() must be capitalized. And


the command line must end with a semicolon (;).

10
System.out.print
public class SecondClass{
public static void main(String[] args){
System.out.print("Hi there!");
System.out.println(”Welcome to APCS A!");
System.out.print(”We will learn Java!");
}
}
Output:
Hi There!Welcome to APCS A!
We will learn Java!

Do you see why there are two lines of output as above?

11
Find the errors.
pooblic class Errors
public static void main(String args){
System.out.print("Good morning! ")
system.out.print("Good afternoon!);
System.Print "And good evening!";

See next slide for all of the corrections.

12
Corrected!

public class Errors {


public static void main(String[] args){
System.out.print("Good morning! ");
System.out.print("Good afternoon!”);
System.out.print("And good evening!”);
}
}

13
Strings
• string: A sequence of characters to be printed.
– Starts and ends with a " quote " character.
• The quotes do not appear in the output.

– Examples: A string enclosed in quotes


is called a string literal.
"hello"
"This is a string. It's very long!"

• Restrictions:
– May not span multiple lines.
"This is not
a legal String."

– May not contain a " character.


"This is not a "legal" String either."
14
Comments
• comment: A note written in source code by the programmer
to describe or clarify the code.
– Comments are not executed when your program runs.
• Syntax:
// comment text, on one line
or,
/* comment text; may span multiple lines */

• Examples:
// This is a one-line comment.
/* This is a very long
multi-line
comment. */

15
Using comments
• Where to place comments:
– at the top of each file (a "comment header")
– at the start of every method (seen later)
– to explain complex pieces of code

• Comments are useful for:


– Understanding larger, more complex programs.
– Multiple programmers working together, who must understand
each other's code.

16
Comments example
/* Suzy Student, CS 101, Fall 2019
This program prints lyrics about ... something. */

public class BaWitDaBa {


public static void main(String[] args) {
// first verse
System.out.println("Bawitdaba");
System.out.println("da bang a dang diggy diggy");
System.out.println();

// second verse
System.out.println("diggy said the boogy");
System.out.println("said up jump the boogy");
}
}

17
Indent Nicely!
public class Welcome{ public static void main(String[]
args){ System.out.println("Hi there!”
);System.out.println(”Welcome to APCS A!");}}

The code above will compile and run correctly. Java ignore whitespaces.
But it is very hard to read, please make an effort to indent nicely!

public class Welcome{


public static void main(String[] args){
System.out.println("Hi there!");
System.out.println(”Welcome to APCS A!");
}
}

18
Lab 1
Create a new repl on your repl.it account and write a program
that has the following outputs:

You must use exactly 5 different print statements.(println and/or


print).

Output:
I am Sam. Sam I am. I do not like them, Sam-I-am.
I do not like green eggs and ham.

19

You might also like