Java Basics.

Slides:



Advertisements
Similar presentations
 2005 Pearson Education, Inc. All rights reserved Introduction.
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Hello, world! Dissect HelloWorld.java Compile it Run it.
Basic Elements of C++ Chapter 2.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Introduction to Information and Computer Science Computer Programming Lecture c This material (Comp4_Unit5c), was developed by Oregon Health and Science.
CS107 Introduction to Computer Science Java Basics.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Java Basics. Java High-level language  More readable for humans  Need to be translated to machine language for execution Compilers  CPU-independent.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Introduction to Computer Systems and the Java Programming Language.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Primitive Data Types. Identifiers What word does it sound like?
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Chapter Topics The Basics of a C++ Program Data Types
4. Java language basics: Function
Chapter 2 Basic Computation
Building Java Programs
Introduction to Computer Science / Procedural – 67130
Yanal Alahmad Java Workshop Yanal Alahmad
Basic Elements of C++.
Data types and variables
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2 Basic Computation
First Programs CSE 1310 – Introduction to Computers and Programming
Basic Elements of C++ Chapter 2.
Chapter 2.
Introduction to Programming in Java
Building Java Programs Chapter 2
Introduction to C++ Programming
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Chapter 2: Basic Elements of Java
Building Java Programs
Chapter 2: Java Fundamentals
Building Java Programs Chapter 2
Introduction to Java Brief history of Java Sample Java Program
Chapter 2 Programming Basics.
Building Java Programs
Introduction to Primitives
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Building Java Programs
In this class, we will cover:
Primitive Types and Expressions
Unit 3: Variables in Java
Building Java Programs Chapter 2
Building Java Programs
Variables and Constants
Presentation transcript:

Java Basics

Java High-level language Designed by Sun Microsystems in 1995 More readable for humans Need to be translated to machine language for execution Compilers CPU-independent translation can target different CPUs (machine languages) Designed by Sun Microsystems in 1995 Sun was bought by Oracle in 2010 Designed with internet in mind Can run in a web browser

Storing Data To store data we need to allocate space in the memory Declare (specify) Type what kind of data Name we can refer to it later Essentially a named location in the memory

Types int double boolean char (signed) integer double-precision floating point number boolean true or false char character

Names (“Identifiers”) Starts with a letter After that, can include letter digit Can these be names? numberOfStudents five5 55 5five Case sensitive Balance and balance are different names Meaningful names improve readability reduce mistakes

The Famous/Weird Semicolon Is similar to a period after a sentence in English End of one instruction Period is used to mean something else in Java Allocating space (“declaration”): int numberOfStudents; double temperature, humidity, pressure; boolean sunny, hurricane; char letterGrade; They are usually called variables similar to math How do we vary/change the value?

Changing Values Assignment x = 97.5; = Equal sign, but doesn’t mean equal as in math x = 97.5; Means assign 97.5 to x (or store 97.5 in x) Doesn’t mean we state x is equal to 97.5

Changing Values Assignment x = 97.5; x = 97.5 + x; = Equal sign, but doesn’t mean equal as in math x = 97.5; Means assign 97.5 to x (or store 97.5 in x) Doesn’t mean we state x is equal to 97.5 x = 97.5 + x; Why is this impossible in math? What does this mean in Java?

Changing boolean and char variables boolean sunny; sunny = false; char letterGrade; letterGrade = ’A’;

Initializing Variables Combining Declaring a variable (allocating space) and Assigning an initial value int numberOfStudents = 15; double gpa = 3.14; char letterGrade = ’A’; boolean sunny = true;

Manipulating Data Operators Arithmetic Relational Logical

Arithmetic Operators + - * / % ++x , x++ Yields a number modulo/reminder 5 % 2 is 1 ++x , x++ Increment x (int) Yields a number

Arithmetic: Division with Integers Math: 5 / 2 is 2.5 Java “integer division”—both values/operands are integers 5 / 2 has an integer value -- floor of 5/2 5 / 2 is 2 [sometimes this is useful] If we want a floating point value (2.5) 5 / 2.0 , 5.0 / 2 , or … Be careful int x = 5 / 2.0 ; x has 2 because 2.5 can’t fit into an int variable

Relational Operators < <= > >= == != Yields true or false value 5 < 2 yields false not stating 5 is less than 2 (in math), which is impossible x == 2 Means what?

Logical Operators && || ! Yields true or false value and or not true && false is false !(5 > 2) is false

&& (and) operator Green (boolean) Square Green && Square F T

|| (or) operator Green (boolean) Square Green || Square F T

! (or) operator Green (boolean) !Green F T

Precedence/Ordering of Operators x < y + z (x < y) + z x < (y + z)

Precedence/Ordering of Operators x < y + z (x < y) + z x < (y + z) x < y + z && y < z x < (y + z) && y < z ((x < (y + z)) && y) < z (x < (y + z)) && (y < z)

Precedence/Ordering of Operators Quite natural Arithmetic (calculate numbers) before Relational (compare numbers) before Logical (combine boolean--true/false values) If not sure, add parentheses

Comments Ignore by the compiler Improves readability, fewer mistakes // describe something that is not obvious /* this is a multi-line comment */

Math Constants and Functions Math.PI, Math.E Math.abs(x) Math.sqrt(x), Math.pow(x, exp) Math.log(x), Math.log10(x) Math.sin(x), Math.cos(x), Math.tan(x) // radians Math.asin(x), Math.acos(x), Math.atan(x) Math.random() // 0 <= num < 1

Input from the Keyboard We’ll usually provide templates for input Scanner keyboard = new Scanner(System.in); x = keyboard.nextInt(); y = keyboard.nextDouble();

Output to the Screen System.out.println( … ); System.out.print( … ); Print the parameter followed by a new line Examples: System.out.println(15); System.out.println(x); System.out.println(“Hello!”); // “string” System.out.print( … ); Print the parameter without a new line