Java Bytecode (binary file)

Similar documents
CS 231 Data Structures and Algorithms, Fall 2016

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

CS 177 Recitation. Week 1 Intro to Java

Computer Programming, I. Laboratory Manual. Final Exam Solution

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Full file at

Program Fundamentals

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Lecture 02, Fall 2018 Friday September 7

CS 11 java track: lecture 1

Key Differences Between Python and Java

Computer Components. Software{ User Programs. Operating System. Hardware

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

An overview of Java, Data types and variables

CS11 Java. Fall Lecture 1

PROGRAMMING FUNDAMENTALS

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Lab # 2. For today s lab:

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Getting started with Java

Getting started with Java

Introduction to Programming Using Java (98-388)

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Basics of Java Programming

( &% class MyClass { }

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Pace University. Fundamental Concepts of CS121 1

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Computer Components. Software{ User Programs. Operating System. Hardware

Introduction to Java. Java Programs Classes, Methods, and Statements Comments Strings Escape Sequences Identifiers Keywords

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Getting Started with Java. Atul Prakash

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Following is the general form of a typical decision making structure found in most of the programming languages:

Lecture Set 2: Starting Java

4 Programming Fundamentals. Introduction to Programming 1 1

Brief Summary of Java

Lecture Set 2: Starting Java

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Basic Concepts. Computer Science. Programming history Algorithms Pseudo code. Computer - Science Andrew Case 2

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

CS260 Intro to Java & Android 03.Java Language Basics

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Chapter 2: Programming Concepts

Object-Oriented Programming

Java Programming. Atul Prakash

Object-oriented programming in...

Programming with Java

17 Hello world 18 Type: String: literal 19 Standard API: System: out.println() 20 Hello world 21 Statement 22 Statement: simple statements are ended w

MODULE 02: BASIC COMPUTATION IN JAVA

FRAC: Language Reference Manual

List of Slides 1 Title 2 Chapter 2: Sequential execution and program errors 3 Chapter aims 4 Section 2: Example:Hello world 5 Aim 6 Class: programs ar

Section 2.2 Your First Program in Java: Printing a Line of Text

Introduction Basic elements of Java

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Course Outline. Introduction to java

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language

Program Development. Program Development. A Foundation for Programming. Program Development

Programming Language Basics

1 Lexical Considerations

Software and Programming 1

Introduction to Java

Object Declaration. <class name>: the name of the class to which the object belongs <object name>: the name of the object (any valid identifier)

1 Introduction Java, the beginning Java Virtual Machine A First Program BlueJ Raspberry Pi...

CHAPTER 2 Java Fundamentals

COMP 110 Project 1 Programming Project Warm-Up Exercise

Programming Language Concepts: Lecture 2

Array. Prepared By - Rifat Shahriyar

Chapter 3 Intro to Java

2 rd class Department of Programming. OOP with Java Programming

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

These are notes for the third lecture; if statements and loops.

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Goals. Java - An Introduction. Java is Compiled and Interpreted. Architecture Neutral & Portable. Compiled Languages. Introduction to Java

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

SSOL Language Reference Manual

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Ruby: Introduction, Basics

A variable is a name that represents a value. For

Elementary Programming

Ruby: Introduction, Basics

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Primitive Data, Variables, and Expressions; Simple Conditional Execution

M e t h o d s a n d P a r a m e t e r s

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

13 th Windsor Regional Secondary School Computer Programming Competition

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

Typescript on LLVM Language Reference Manual

Transcription:

Java is Compiled Unlike Python, which is an interpreted langauge, Java code is compiled. In Java, a compiler reads in a Java source file (the code that we write), and it translates that code into bytecode. Bytecode is an intermediate file format that represents low-level Java operations in a machine-independent way. The Java virtual machine then reads the bytecode and executes our program. Java Source Code (text file) Compiler (javac) Java Bytecode (binary file) Java Runtime Env. (java) Practically, this means we write some code in our favorite text editor, run the javac compiler on that code to create a.class file, then run that file in the Java runtime environment using the java command. $ atom HelloWorld.java $ ls HelloWorld.java $ javac HelloWorld.java $ ls HelloWorld.class HelloWorld.java $ java HelloWorld <output of HelloWorld.java program> This also means that there is no REPL in Java. We must write an entire program, compile that program, and run that program. For every change we make, we must recompile. This limits our ability to rapidly prototype our code. Punctuation In Python, whitespace matters: indentation defines the scope of our code, and most control structures end with a colon (:). Java syntax relies much more heavily on punctuation. Every line must end in a semicolon (;). As we will see later, we use curly brace pairs to define scope ({), and parentheses in most of our control structures. Comments are also defined differently. Python uses the hash (#), but Java actually has two types of comments: The double backslash defines a single-line comment: // Multi-line comments are enclosed in dot-slash pairs: /* */ Standard Output and Printing At this point, we are very familiar with the Python3 print() function: print() is a very useful way to debug our code by writing values to standard output. In Java, we have the System.out.print() and System.out.println() functions instead. System.out.println() adds a newline at the end of each print. System.out.println("Hello World"); Fall Semester 2016 1 CS 135: Diving into the Deluge of Data

Java and Types Java has builtin types that are similar to builtin Python types, but numeric Java types have a fixed size. This means that we must know the largest value that we plan to represent, and use a large-enough type to store that value. byte: 8-bit integer short: 16-bit integer int: 32-bit integer long: 64-big integer float: 32-bit real number double: 64-bit real number boolean: false and true char: single character (declared with single quotes) In addition to these builtin types, the Java String and Vector classes are similar to Python s str and list. However, Java String literals must be defined with double quotes ( ), and every element in a Vector must be of the same type. String is part of the standard Java API, but the Vector class must be imported. To import a class, you use the import keyword: import java.util.vector; This imports the Vector class from the java.util package. Java is a Statically Typed Langauge Python gives us a lot of flexibility as programmers, but as we have seen during the semester, subtle bugs often go unnoticed until our programs throw a TypeError exception at runtime. In Java, the compiler checks the types of our variables, verifies that they are compatible, and outputs errors before we are able to run our code. Because of static typing, we must declare the type of every variable before assigning it a value, and once we declare a variable s type, its type cannot change. int a; String s = "text"; We often declare our variables at the top of a scope and then assign values later, but this is just a throwback to how things are done in C. The only requirement is that we declare a variable somewhere before we use it. For certain types, we can do what is called casting. We cast a value from one type to another by specifying the new type in parentheses before the value. This is legal in many cases, but sometimes casting will cause our programs to raise a ClassCastException. The compiler cannot know when this will happen beforehand; ClassCastExceptions are runtime errors. int a = 3; long b = (long) a; Object c = "Everything is a subclass of type Object"; String s = (String) c; Fall Semester 2016 2 CS 135: Diving into the Deluge of Data

Booleans, Conditionals, and Comparisions Python uses if/elif/else clauses. Java instead uses if/else if/else. In Java, the condition must be enclosed in Parentheses, and the conditional statement does not end in a colon. However, if your code block contains more than one line, you must enclose the entire block in braces. if (a == true) { i = i + 1; System.out.prinln("This multiline block required braces"); else if (i < j) { i = 0; else { return i; Python boolean expressions are combined using and and or, and negation is done using not. Java instead uses symbols: && (logical and), (logical or), and! (logical not). Again, this is an instance of Java being similar to C. Comparision of == and!= is different in Java as well. In Python, this checked if two objects had the same value. In Java these check if two objects are the same object. Loops Java loops are logically similar, but quite different in syntax. For There are actually multiple ways to construct a for loop in Java. Here is a code snippet uses two of those ways. It fills a Vector of integers with the numbers 0-10 and then prints those integers to standard output: Vector<Integer> v = new Vector<Integer>(); // for (initialization; stopping condition; update) for (int i = 0; i < 10; i++) { v.add(i); // for (type element : iterable_collection) for (int i : v) { System.out.println(i); The first for loop shows the classic way to iterate over indices (although it uses the Vector class s add() method to append to the collection instead of indexing into the Vector using bracket ([]) notation). Everything before the first semicolon is executed exactly once, immediately before beginning the first loop iteration. This is a good place to declare and initialize local loop variables. The boolean expression between the next two semicolons is executed before each loop iteration. If the expression evaluates to false, the loop body is skipped. Everything that follows the last semicolon is executed after each iteration; this is a good place to update any loop variables, often by incrementing/decrementing. The second for loop is similar to the for thing in things syntax in Python. It declares a local variable of the type that is stored in the iterable collection (here, Vector v stores Integers, so we declare a local Integer i), and then it executes the loop body once for each object in that collection. Fall Semester 2016 3 CS 135: Diving into the Deluge of Data

While Java has two while loops: the while and the do-while. The difference is when the loop condition is checked. In a while loop, the check happens first. while (expression) { statement(s); In a do-while loop, the check happens last. do { statement(s); while (expression); Thus, a do-while loop is guaranteed to execute at least once. Classes Unlike Python, which supports Object Oriented Programming but does not require it, everything in Java must be inside a class body. Classes are defined with the class keyword, and class names start with a capital letter and use the CapWords style. The class body is contained with in curly braces ({). class MyClass { <class body goes here> Classes can have member variables associated with them to represent their internal state. In Java, member variables are declared in the top-level class body. To refer to a class variable within a function, you can use the this keyword (you can think of this like the self convention in Python, except this is part of the language), but this is not required. You can also declare member variables as public, private, or protected. import java.util.vector; class MyClass { public int i; private Vector<String> v; protected char c = x ; <rest of class body goes here> Functions A function specifies its visibility (public, private, protected), its return type, and a formal parameter list (each parameter must also specify a type). Here is an example of a simple function to add two numbers: public int addnums(int a, int b) { return a + b; Java functions must always appear inside a Java class. A function is called by specifying an object of the class and using a dot (.). If the function does not depend on the internal state of an object, the function can be declared as a static function. To call a static function, use the name of the class followed by a dot (.). Unlike Python, we can define mulitple Java functions with the same name as long as they have different arguments (types or number). The arguments are used to decide which function to call. This is called overloading. Fall Semester 2016 4 CS 135: Diving into the Deluge of Data

The main method For a Java program to be executable, we must define a main method. The main method must be public and static, and it takes an array of Strings as its argument. The String array is how command line arguments are passed into the program. class HelloWorld { public static void main(string[] args) { System.out.println("Hello World!"); Fall Semester 2016 5 CS 135: Diving into the Deluge of Data