Getting started with Java

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

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

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

CMPT 125: Lecture 3 Data and Expressions

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

JAVA Programming Fundamentals

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

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

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

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Zheng-Liang Lu Java Programming 45 / 79

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

Full file at

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

Full file at

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

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Programming. Syntax and Semantics

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

Java Basic Datatypees

Basics of Java Programming

Visual C# Instructor s Manual Table of Contents

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

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

Elementary Programming

Programming with Java

Values and Variables 1 / 30

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

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Chapter 2 Elementary Programming

PROGRAMMING FUNDAMENTALS

CS 106 Introduction to Computer Science I

More about Binary 9/6/2016

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

Program Fundamentals

Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes

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

Chapter 6 Primitive types

Course Outline. Introduction to java

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

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

Fall 2017 CISC124 9/16/2017

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

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

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

4 Programming Fundamentals. Introduction to Programming 1 1

Computational Expression

Lecture Set 2: Starting Java

ARG! Language Reference Manual

Lecture Set 2: Starting Java

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

Declaration and Memory

Chapter 2: Data and Expressions

Hello World. n Variables store information. n You can think of them like boxes. n They hold values. n The value of a variable is its current contents

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

Syntax and Variables

CIS 110: Introduction to Computer Programming

Java Classes & Primitive Types

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

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

An overview of Java, Data types and variables

Introduction to Java & Fundamental Data Types

Strings, Strings and characters, String class methods. JAVA Standard Edition

COMP 110 Introduction to Programming. What did we discuss?

CS11 Java. Fall Lecture 1

Chapter 2 Primitive Data Types and Operations. Objectives

Java Classes & Primitive Types

Java Bytecode (binary file)

Object-Oriented Programming

Software Practice 1 Basic Grammar

Java Identifiers. Java Language Essentials. Java Keywords. Java Applications have Class. Slide Set 2: Java Essentials. Copyright 2012 R.M.

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

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

TUGCE KEMEROZ - ASLI OZKAN - AYSE TARTAN. Week 12/02/ /02/2007 Lecture Notes:

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

What did we talk about last time? Examples switch statements

Chapter 02: Using Data

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.

VARIABLES AND TYPES CITS1001

Princeton University. Computer Science 217: Introduction to Programming Systems. Data Types in C

Software Practice 1 - Basic Grammar Basic Syntax Data Type Loop Control Making Decision

CIS133J. Working with Numbers in Java

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

1007 Imperative Programming Part II

printf( Please enter another number: ); scanf( %d, &num2);

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

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Section 2: Introduction to Java. Historical note

Peer Instruction 1. Elementary Programming

Chapter 2 ELEMENTARY PROGRAMMING

COMP6700/2140 Data and Types

Variables, Constants, and Data Types

Chapter 2: Data and Expressions

CT 229 Fundamentals of Java Syntax

Java Identifiers, Data Types & Variables

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Transcription:

Getting started with Java

Magic Lines public class MagicLines { public static void main(string[] args) { } }

Comments Comments are lines in your code that get ignored during execution. Good for leaving explanations of your code For other programmers For yourself in 5 months Good for suppressing one snippet of code when you want to try an alternative snippet Indicated by //your comment here (single line) /* your comment here */ (multiple lines)

Comments public class MagicLines { public static void main(string[] args) { // Comment: Execution begins here /* Comment: Continues here */ } /* Comment: * Ends * here */ }

Text output System.out.print(your_output_here) Prints your output in the console System.out.println(your_output_here) Subsequent output will be on the next line System.out.format(your_output_here) Apply fancy formatting to output before printing Like printf from C (You don t need to know this one)

Text output public class MagicLines { public static void main(string[] args) { System.out.println( Start here ); System.out.println( Continue ); System.out.println( Stop + here ); } }

Strings of Text A sequence of letters is called a String: Start here Continue Stop here Strings can be concatenated with +: Stop + here is the same as Stop here Numbers can be concatenated too. These are the same: Stop + 3 Stop 3

Strings of Text Punctuation and other characters allowed \tstart here! (\t results in a tab) Strings are a type of data. Your output in System.out.print() and println() can be other types (like numbers), not just Strings: System.out.print(3);

Managing Data Save the data Hello, which is a string, in a variable named str: String str = Hello ; Data type Variable Literal

Data types Primitive types Basic data like numbers or letters Require just a few contiguous bytes Objects More complicated data like Strings of letters or arbitrarily precise numbers (BigDecimals) Require many bytes of storage Defined with many more lines of code More complex behavior

Primitive Data-types Logical boolean : Two values, true or false Textual char : Single character ( a, b, ) 16 bits (65536 possible characters)

Primitive Data-types Integral byte : 8-bit integer in [-128, 127] short : 16-bit integer in [-32768, 32767] int : 32-bit integer in [-2,147,483,648, 2,147,483,647] long : 64-bit integer in [-9,223,372,036,854,775,808, 9,223,372,036,854,775,807 ]

Representing negative numbers Binary Decimal (Unsigned) Decimal (Signed) 000 0? 001 1? 010 2? 011 3? 100 4? 101 5? 110 6? 111 7?

Signed two s complement Binary Decimal (Unsigned) Decimal (Signed) 000 0 0 001 1 1 010 2 2 011 3 3 100 4-4 101 5-3 110 6-2 111 7-1 Leftmost digit indicates sign Splits in the middle Wraps around at the ends

Primitive Data-types Floating point float : 32-bit rational numbers Ranges from ~ ± 10 45 to ~ ± 10 38 double : 64-bit rational numbers Ranges from ~ ± 10 324 to ~ ± 10 308 How to represent in binary?

Primitive Data-types Floating point float : 32-bit rational numbers Ranges from ~ ± 10 45 to ~ ± 10 38 double : 64-bit rational numbers Ranges from ~ ± 10 324 to ~ ± 10 308 Uses the IEEE 754 floating point standard Think scientific notation in base 2: 1.2 10 3 1.01101 2 10010100

Literals Literals are constant values that are hardcoded into the program: int x = 123; Data type Variable Literal

Literals boolean (case sensitive): true false char Single quote, single letter: a Single quote, single escape sequence. Backspace: \b Tab: \t Single quote, Unicode escape sequence: \u00f1 (n with a tilde) 16-bit positive integer in [0, 65535]

Literals String Double quote, multiple letters and escape sequences: Hello \t \u00f1 Hello\t\u00F1

Literals Integral: byte, short, int Base 10: 123 Base 2 (prefix with 0b): 0b01111011 Base 16 (prefix with 0x): 0x7B Underscores allowed between digits: 999_999_999 long: suffix with L (or l): 2_147_483_648L

Literals Floating point (float) Suffix with F (or f): 123.4f Floating point (double): With decimal point: 123.4 Optional suffix D (or d): 123.4d Scientific notation using E (or e): 1.234 10 20 1.234e20

Variables A name that denotes some value. The value of a variable can change over the course of program execution. int x = 123; Data type Variable Literal

Declaring variables All variables must be declared, before they can refer to a value. The declaration determines the data type. The value of a variable can change, but the data-type cannot. int x; float y; String z;

Assigning to variables To reset the value of a variable, a new value must be assigned. Assignment is done using the assignment operator (equal sign): x = 3; y = 3.0; z = 3.0 ;

Assigning to variables Assignment is not the same as logical equality!!!! int x; //Declare x x = 3; //The value of x is 3 x = 4; //It is also 4???

Assigning to variables Assignment is not the same as logical equality!!!! 1 2 3 int x; //Now x is declared x = 3; //Now the value of x is 3 x = 4; //Now the value of x is 4

Initializing variables You can declare a variable and assign a value to it in the same line. This is called initialization: int x = 3; float y = 3.0; String z = 3.0 ;

Type casting Converting data from one type to another is called type casting. Syntax: precede your value with the new type (in parentheses). float y; y = (float) 3; //Now y is 3.0

Type casting Type casting can destroy data: int i = (int) 3.3; Removes fractional part (3.3 becomes 3) byte b = (byte) 0b101_1000_0001; Removes most significant bytes 0b101_1000_0001 becomes 0b1000_0001 Only necessary when data might be destroyed float f = 3; //No error int i = 3.0; //Error

Lab tomorrow Bring your laptops! You can get started early by visiting the UMCP CS Department Eclipse tutorial: http://www.cs.umd.edu/eclipse/ (This is also linked on the resources page of the class website) Follow the steps under the Installing Eclipse section