CMSC131. Introduction to your Introduction to Java. Why Java?

Similar documents
Lecture Set 2: Starting Java

Lecture Set 2: Starting Java

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

What did we talk about last time? Examples switch statements

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

Programming with Java

Program Fundamentals

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

COMP 110 Introduction to Programming. What did we discuss?

Computational Expression

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

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Chapter 2 ELEMENTARY PROGRAMMING

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

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

Chapter 2 Elementary Programming

CS 106 Introduction to Computer Science I

4 Programming Fundamentals. Introduction to Programming 1 1

CS 11 java track: lecture 1

Fundamentals of Programming Data Types & Methods

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

B.V. Patel Institute of BMC & IT, UTU 2014

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

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

Full file at

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Primitive Data, Variables, and Expressions; Simple Conditional Execution

A variable is a name for a location in memory A variable must be declared

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

Getting started with Java

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

Gaddis: Starting Out with Java: From Control Structures through Objects, 6/e

First Java Program - Output to the Screen

Important Java terminology

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

Starting Out with Java: From Control Structures through Data Structures 3e (Gaddis and Muganda) Chapter 2 Java Fundamentals

Section 2: Introduction to Java. Historical note

Oct Decision Structures cont d

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

13 th Windsor Regional Secondary School Computer Programming Competition

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

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

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Chapters 1-4 Summary. Syntax - Java or C? Syntax - Java or C?

Chapter. Let's explore some other fundamental programming concepts

Chapter 2: Data and Expressions

4. If the following Java statements are executed, what will be displayed?

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

Chapter 2: Data and Expressions

ECE 122 Engineering Problem Solving with Java

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

Building Java Programs

Elementary Programming

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

Full file at

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

Chapter 4: Control Structures I

Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

CS 302: Introduction to Programming

Object Oriented Programming. Java-Lecture 1

COMP 202 Java in one week

Building Java Programs

Getting started with Java

Chapter 2 Primitive Data Types and Operations. Objectives

Datatypes, Variables, and Operations

Programming Language Concepts: Lecture 2

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings

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

Chapter 2. Elementary Programming

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

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

CP122 Computer Science I. Binary, Strings, Conditionals, User Input

Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Chapter 4: Conditionals and Recursion

Full file at

JAVA OPERATORS GENERAL

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Chapter 2 Primitive Data Types and Operations

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

COMP Primitive and Class Types. Yi Hong May 14, 2015

CMPT 125: Lecture 3 Data and Expressions

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Java Bytecode (binary file)

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Basics of Java Programming

COMP 202. Java in one week

Zheng-Liang Lu Java Programming 45 / 79

CHAPTER 2 Java Fundamentals

Introduction to Java Applications; Input/Output and Operators

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

Course Outline. Introduction to java

Text User Interfaces. Keyboard IO plus

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

CIS 110: Introduction to Computer Programming

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

CS 177 Spring 2010 Exam I

Transcription:

CMSC131 Introduction to your Introduction to Java Why Java? It s a popular language in both industry and introductory programming courses. It makes use of programming structures and techniques that can be transferred to using a variety of other languages. Development on different platforms without platform-specific differences for what we will be doing is more straight-forward. 1

Phrases you might hear Garbage collection when we talk about requesting memory to hold information for our program to use Cross-platform executables the modules we build and even full applications can be used under different operating systems due to the Java virtual machine The JVM The Java compiler does not compile to the machine code of an actual physical machine architecture, but rather to bytecode which is run on a Java Virtual Machine. If you compile a JVM for a new operating system, then (in theory) all of your existing Java programs could run there too. There are times when (for efficiency) Java bytecode is actually compiled to a specific architecture s machine code (no longer portable). 2

Java to Bytecode to Execution http://support.novell.com/techcenter/articles/img/ana1997070102.gif How many bytes in a kilobyte? 0% 1. 8 0% 0% 0% 0% 2. 128 3. 1000 4. 1024 5. none of the above 45 0 of 5 3

HelloWorld2.java example public class HelloWorld2 { System.out.println("Hello World!"); mymethod("hi", "There"); public static void mymethod(string first, String second) { System.out.println(first+" "+second); System.out.println(second+" "+first); Methods (operations that can be performed) public static void main(string[] args) public static void mymethod(string first, String second) HelloWorld2.java example public class HelloWorld2 { System.out.println("Hello World!"); mymethod("hi", "There"); public static void mymethod(string first, String second) { System.out.println(first+" "+second); System.out.println(second+" "+first); Operands/Parameters (data the method uses) String[] args String first, String second 4

HelloWorld2.java example public class HelloWorld2 { System.out.println("Hello World!"); mymethod("hi", "There"); public static void mymethod(string first, String second) { System.out.println(first+" "+second); System.out.println(second+" "+first); Output Commands (in this case to the console) System.out.println("Hello World!"); System.out.println(first+" "+second); System.out.println(second+" "+first); Output We will see two basic output options for sending text to the console: System.out.println System.out.print The difference between the two is that the first one adds an end of line character at the end of whatever it has sent to the console. You can request the contents of variables to be displayed (which a variable may or may not support) or you can ask for a literal string to be displayed (contained in quotation marks in your output command). 5

HelloWorld2.java example public class HelloWorld2 { System.out.println("Hello World!"); mymethod("hi", "There"); public static void mymethod(string first, String second) { System.out.println(first+" "+second); System.out.println(second+" "+first); Method Call mymethod("hi", "There"); Method Signatures public static void mymethod(string first, String second) public: we will discuss visibility later static: we will discuss this later as well void: the type of the value the method will return (in this case it does not return a value so we say the type is void ) mymethod: method name String first, String second: defining memory spaces and local names for values that will be passed into the method 6

public class DoSomeMath { DoSomeMath.java example int x; int y; x = 17; y = 23; printsum(x,y); printprod(x,y); printquot(x,y); printquot(y,x); Variable Declarations Commands/Instructions public static void printsum(int first, int second){ System.out.println(first+second); public static void printprod(int alpha, int beta){ System.out.println(alpha*beta); public static void printquot(int alpha, int second){ System.out.println(alpha/second); Variables In the DoSomeMath example, there were some local variables declared int x; int y; assigned to x = 17; y = 23; and used printsum(x,y); In short, variables have a data type (such as int) and refer to space within the computer s memory where their values (such as 17) are stored. You can assign values to them and read those values back out from them. 7

AddOne.java example public class AddOne { int x; x = 17; System.out.println(x); printplus1(x); System.out.println(x); public static void printplus1(int val){ val = val + 1; //NOTE: Due to how the int data type works, // x back in the calling method doesn't change! System.out.println(val); Comments There are times that you will want to leave notes for yourself and other programmers within the code without it being executed. This is where comments come in! There are two types of comments in Java: /* comment goes until the close */ /* comment goes until the close which might not be on the same line as where the open appears */ // comment goes from start to the end of that line 8

Some Primitive Types Integer values (and how many bytes they get) byte (1), short (2), int (4), long (8) Real numbers (and how many bytes they get) float (4), double (8) Individual Unicode characters char (2) Boolean truth values boolean (1) Range of Values The range of values depends on several things, one of which is how much memory is available for storing the value and another is how Java interprets the 0s and 1s stored in that memory. A byte is one byte in size and can store a value between -128 and +127 but boolean is also one byte in size yet it can only store two values; true or false. An int is four bytes in size and can store a value between -2,147,483,648 and +2,147,483,647 but a float is also four bytes in size yet it can store numbers between (roughly) -3.4x10 38 and 3.4x10 38 (though only with 7 digits of precision). 9

If a short integer holds values from -32,768 to 32,767, what happens if it is holding 32,767 and you add one? 0% 1. The program crashes. 0% 2. The program automatically allocates a larger integer to hold larger values. 0% 0% 0% 3. The value becomes 32,767. 4. The value becomes -32,768. 5. The value becomes 0. 45 0 of 5 float -vs- double By default, real number values are assumed to be "double" values. float val = 17.5; won't work it needs to be float val = 17.5F; 10

Some Math Operations Addition: + Subtraction: - (also used as unary negative ) Multiplication: * Division: / Integer division discards remainder 17 / 5 yields 3 Modulus: % Returns what the remainder would be if the two operands were divided 17 % 5 yields 2 Numeric Comparison Operators Less than: < Greater than: > Less than or equal to: <= Greater than or equal to: >= Is equal to: == Is not equal to:!= NOTE = is for assignment, == is for comparison 11

Comps.java example public class Comps { boolean flag; flag = (7<14); System.out.println(flag); flag = (17<14); System.out.println(flag); flag = (17=14); //THIS WON'T COMPILE The String type The String data type is not a primitive type. It is a Class and when we declare one to use, it is called an Object (we will discuss these ideas more later). As we saw earlier, you can concatenate two String objects using the + operator. Note that the + operator can also be used for math operations. The left operand "decides" which form of + is being used. "S" is a String of length 1 'S' is a char 12

Some Special Character Values single quote: \ double quote: \ new line (crlf): \n tab: \t single backslash: \\ SomeStrings.java example public class SomeStrings { String name = "Sam"; char letter = 'A'; int number1 = 14; float number2 = 17.5F; System.out.println("Hi\nThere\tClass\\\\\n\n"); System.out.println(name+letter+number1+number2); System.out.println(name+letter+(number1+number2)); 13

String Comparison String objects are compared using Methods rather than Operators. string1.equals(string2) will return a boolean value string1.compareto(string2) will return an integer value using the following rules based on the lexicographical order of the strings: If the result is less than 0 string1 precedes string2 If the result is equal to 0 string1 matches string2 If the result is greater than 0 string1 succeeds string2 NOTE the negative value doesn't have to be -1 and the positive value doesn't have to be +1 Input using the Scanner class The Scanner class is included as part of the utility libraries available in Java 5.0 (or later) but we need to import it to make use of it. import java.util.scanner; It allows us to obtain data from an input source (such as a keyboard) and also convert that data into the format of different Java types. 14

Creating and Using We will need to declare and create a Scanner object: Scanner myscanner = new Scanner(System.in); We can then use any of Scanner's methods to read and convert data, such as nextboolean() nextfloat() nextint() next() //reads/returns characters until next whitespace nextline() //reads/returns characters until the end of the input line SimpleInput.java example import java.util.scanner; public class SimpleInput { Scanner myscanner = new Scanner(System.in); int i; float f; String s; i = myscanner.nextint(); System.out.println("The integer was a " + i); s = myscanner.next(); System.out.println("The next \'word\' was " + s); s = myscanner.nextline(); System.out.println("Rest of the line was " + s); 15

Copyright 2010-2012 : Evan Golub 16