Chapter 3 GC 101 Java Fundamentals.

Slides:



Advertisements
Similar presentations
IT151: Introduction to Programming
Advertisements

Dale Roberts Introduction to Java - First Program Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
Chapter 1 Introduction to JAVA. Why Learn JAVA? Java is one of the fastest growing programming language in the world. Java is one of the fastest growing.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Object Oriented Programming in JAVA
Chapter 1: Introduction
1 Procedural decomposition using static methods suggested reading:1.4.
1 Java Basics. 2 Compiling A “compiler” is a program that translates from one language to another Typically from easy-to-read to fast-to-run e.g. from.
Introduction To Computers and Programming Lecture 2: Your first program Professor: Evan Korth New York University.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
How to Create a Java program CS115 Fall George Koutsogiannakis.
CMT Programming Software Applications
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 1 Introduction to Computers and Programming in JAVA.
Introduction to C Programming
Copyright 2013 by Pearson Education Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading:
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 1: Introduction to Java Programming.
CSC 111 Java Programming I. Java Programming: From Problem Analysis to Program Design, Second Edition  Instructor – Salwa Hamad Al-Jasser  Office.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Chapter 2 How to Compile and Execute a Simple Program.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in The.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
JAVA BASICS: Variables and References SYNTAX, ERRORS, AND DEBUGGING.
© 2012 Pearson Education, Inc. All rights reserved. 1-1 Why Java? Needed program portability – Program written in a language that would run on various.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Board Activity Find your seat on the seating chart Login – Remember your login is your first initial your last name and the last three numbers of your.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Introduction to programming in the Java programming language.
8/31: Intro to Java, Languages, and Environments Types of programming languages –machine languages –assembly languages –high-level languages Java environment.
J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, D.S. Malik D.S. Malik.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second Edition D.S. Malik D.S. Malik.
Chapter 1: An Overview of Computers and Programming Languages
Anatomy of a Java Program. AnotherQuote.java 1 /** A basic java program 2 * 3 Nancy Harris, James Madison University 4 V1 6/2010.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
© 2012 Pearson Education, Inc. All rights reserved types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.
COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
1/16: Intro to Java, Languages, and Environments Types of programming languages –machine languages –assembly languages –high-level languages Java environment.
CHAPTER 4 GC 101 Identifiers and Data types. 2 IDENTIFIERS  identifier: A name given to a piece of data, method, etc.  Identifiers allow us to refer.
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
Introduction to 1. What is Java ? Sun Microsystems Java is a programming language and computing platform first released by Sun Microsystems in The.
Copyright 2010 by Pearson Education APCS Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading:
Chapter 6 JavaScript: Introduction to Scripting
Working with Java.
GC101 Introduction to computer and program
Console Output, Variables, Literals, and Introduction to Type
Introduction to.
Chapter 2 Introduction to Java Applications
CSE 190D, Winter 2013 Building Java Programs Chapter 1
Data types and variables
Java programming lecture one
IDENTIFIERS CSC 111.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 1: Computer Systems
Lecture 1: Basic Java Syntax
elementary programming
Anatomy of a Java Program
Building Java Programs
CSE 142, Spring 2012 Building Java Programs Chapter 1
CSE 142, Winter 2014 Building Java Programs Chapter 1
Variables in C Topics Naming Variables Declaring Variables
Computer Programming-1 CSC 111
Instructor: Alexander Stoytchev
Zorah Fung University of Washington, Winter 2016
Presentation transcript:

Chapter 3 GC 101 Java Fundamentals

Processing a Java Program A Java program undergoes several stages : Editing: use Java code and save in a text file named className .java ( source program ). 2. Compiling : the compiler checks the source program for any syntax errors then translates the program into code understood by interpreter called bytecode saved in a file named className.class 3. Loading : the .class file is loaded into computer main memory for execution, and connected to all classes. 4. Verifying : to validate and secure against damage . 5. Interpreting :the Interpreter reads and translates each bytecode instruction into machine language and then executes it , one instruction at a time .

Processing a Java Program

Processing a Java Program Java Virtual Machine (JVM): A hypothetical computer developed to make Java programs machine independent ( i.e run on many different types of computer platforms ). Bytecode is the machine language for the JVM .

Processing a Java Program Two types of Java programs: Applications : standalone programs stored and executed on a local computer . Applets : small programs stored on remote computers that users connect to via a WWW browser. Applets are loaded into the browser , executed then discarded .

What does a Java program look like? Example 1 A simple Java application: an application executes using the Java interpreter. The basic unit of a Java program is a class. Every Java program must have at least one class . Each class begins with a class declaration that defines data and methods for the class . We’ll talk about this more later.

What does a Java program look like? Example 1 Here’s a class called Welcome: public class Welcome { // This is a comment. } It’s a convention that the name of a class starts with a capital letter. You’ll meet other conventions along the way!! At the moment, we’ve defined a class that does nothing. The line with the // in front of it is not translated by the compiler - it’s called a comment and it’s ignored. So let’s make our program do something!

What does a Java program look like? Example 1 public class Welcome { public static void main(String [] args) System.out.print(“Welcome to Java”); } We’ve now added a main method to our class. We’ve also used a number of words that have a special meaning to Java: class, public, static, void and String. The line System.out.print(“Welcome to Java ”) is an instruction to print the sentence Welcome to Java on the screen. The double quotes (“) are not printed out as they are used to inform the compiler that Welcome to Java is a String. Then our program exits.

What does a Java program look like? Example 1 public static void main (String args[]) is a part of every Java application program. Java applications automatically begin executing at main() The void before main() means that main will not return any info . A Java class must contain one main method if it is an application .

What does a Java program look like? Let’s make it work ! 1. Type the program into a text editor 2. Save as Welcome.java 3. Compile into byte codes javac Welcome.java 4. Execute byte codes java Welcome

What does a Java program look like? Let’s work it !

What does a Java program look like? Remember ! Upper case and lower case are very important! Java is case-sensitive. ( A is NOT similar to a) Your class name MUST MATCH YOUR FILE NAME. You only use the class name when you invoke Java but you use the file name when you invoke the compiler (Javac). A file cannot contain two public classes.

What does a Java program look like? Example 2 public class ASimpleJavaProgram { public static void main(String[] args) System.out.println("My first Java program."); System.out.println("The sum of 2 and 3 = " + 5); System.out.println("7 + 8 = " + (7 + 8)); } Class name Body of class Heading of method main

What does a Java program look like? Example 2 A Java output statement causes the program to evaluate whatever is in the parentheses and display the result on screen . + is used to concatenate the strings . The system automatically converts the number 5 into a string ,joins that string with the first string ,and displays it . The parentheses around 7+8 causes the system to add the numbers 7 and 8 ,resulting in 15 . The number 15 is then converted to string 15 and joined with string “7+8”= “ .

Sample Run

Special Symbols + Word Symbols( reserved words) public class Message { public static void main(String[] arg) System.out.println("This is a message"); } Note: Blank is a special symbol. Also called reserved words or keywords. They are words that mean something special to Java. Cannot be redefined. Always lowercase.

Other Special Symbols

Java Reserved Words

Java Identifiers They are names that we introduce in our program public class Message { public static void main(String[] arg) System.out.println("This is a message"); } They are names that we introduce in our program Some are predefined; others are defined by the user. Consists of: Letters: (a  z) ( A  Z) Digits (0  9) The underscore character (_) Must begin with a letter, underscore, or the dollar sign.

Java Identifiers Java identifiers can be any length. Unlike reserved words, predefined identifiers can be redefined, but it would not be wise to do so. Some predefined identifiers: print, println, next, nextLine Names should be descriptive: • Message – the name of a program that prints out a message. • System.out.println – the name for a part of Java that prints a line of output to the screen.

Illegal Identifiers Note: White space, breaks up the program into words, e.g. the two reserved words static void, rather than staticvoid, which would be assumed to be an identifier !

Identifiers identifier: A name given to a piece of data, method, etc. Identifiers allow us to refer to an item later in the program. Identifiers give names to: classes methods variables, constants Conventions for naming in Java: classes: capitalize each word (ClassName) methods: capitalize each word after the first (methodName) (variable names follow the same convention) constants: all caps, words separated by _ (CONSTANT_NAME)

Details about identifiers Java identifiers: first character must a letter or _ or $ following characters can be any of those or a number identifiers are case-sensitive (name is different from Name) Example Java identifiers: legal: susan second_place _myName TheCure ANSWER_IS_42 $variable illegal: me+u 49er question? side-swipe hi there ph.d jim's 2%milk suzy@yahoo.com

Comments comment: A note written in the source code by the programmer to make the code easier to understand. Comments are not executed when your program runs. Most Java editors show your comments with a special color. Comment, general syntax: /* <comment text; may span multiple lines> */ or, // <comment text, on one line> Examples: /* A comment goes here. */ /* It can even span multiple lines. */ // This is a one-line comment.

Using comments Where to place comments: at the top of each file (also called a "comment header"), naming the author and explaining what the program does at the start of every method, describing its behaviour inside methods, to explain complex pieces of code (more useful later)

Comments example /* Suzy Student CS 101, Fall 2019 This program prints lyrics from my favorite song! */ public class MyFavoriteSong { /* Runs the overall program to print the song on the console. */ public static void main(String[] args) { sing(); // Separate the two verses with a blank line System.out.println(); } // Displays the first verse of the theme song. public static void sing() { System.out.println("Now this is the story all about how"); System.out.println("My life got flipped turned upside-down");