Java Data Types: Strings, Integers, Floating-Point Numbers, Booleans, and Operations, Slides of Computer Programming

An overview of built-in data types in java, including strings, integers, floating-point numbers, and booleans. It covers the basic definitions of variables and assignment statements, as well as operations such as addition, subtraction, multiplication, division, and modulo. The document also includes examples of string concatenation, integer operations, and the use of java's math library for floating-point numbers.

Typology: Slides

2011/2012

Uploaded on 07/15/2012

sachdew
sachdew 🇮🇳

4

(6)

37 documents

1 / 23

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1.2 Built-in Types of Data
!
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17

Partial preview of the text

Download Java Data Types: Strings, Integers, Floating-Point Numbers, Booleans, and Operations and more Slides Computer Programming in PDF only on Docsity!

1.2 Built-in Types of Data

!

2

Built-in Data Types

Data type. A set of values and operations defined on those values.

add, subtract, multiply, divide

6.022e floating-point numbers double add, subtract, multiply, divide 17 12345 int integers and, or, not true false boolean truth values sequences of characters characters type set of values literal values operations compare 'A' '@' char String (^) concatenate "Hello World" "126 is fun"

4

Trace

Trace. Table of variable values after each statement.

Text

7

Subdivisions of a Ruler

% java Ruler 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 "1 2 1" "1 2 1 3 1 2 1" "1" public class Ruler { public static void main(String[] args) { String ruler1 = "1"; String ruler2 = ruler1 + " 2 " + ruler1; String ruler3 = ruler2 + " 3 " + ruler2; String ruler4 = ruler3 + " 4 " + ruler3; System.out.println(ruler4); } } (^) string concatenation

Integers

10 public class IntOps { public static void main(String[] args) { int a = Integer.parseInt(args[ 0 ]); int b = Integer.parseInt(args[ 1 ]); int sum = a + b; int prod = a * b; int quot = a / b; int rem = a % b; System.out.println(a + " + " + b + " = " + sum); System.out.println(a + " * " + b + " = " + prod); System.out.println(a + " / " + b + " = " + quot); System.out.println(a + " % " + b + " = " + rem); } }

Integer Operations

command-line arguments Java automatically converts a, b , and rem to type String % javac IntOps.java % java IntOps 1234 99 1234 + 99 = 1333 1234 * 99 = 122166 1234 / 99 = 12 1234 % 99 = 46

Floating-Point Numbers

13

Excerpts from Java’s Math Library

http://java.sun.com/javase/6/docs/api/java/lang/Math.html

14

Quadratic Equation

public class Quadratic { public static void main(String[] args) { // parse coefficients from command-line double b = Double.parseDouble(args[ 0 ]); double c = Double.parseDouble(args[ 1 ]); // calculate roots double discriminant = bb - 4.0c; double d = Math.sqrt(discriminant); double root1 = (-b + d) / 2.0; double root2 = (-b - d) / 2.0; // print them out System.out.println(root1); System.out.println(root2); } }

Ex. Solve quadratic equation x^2 + bx + c = 0.

roots = " b ± b 2 " 4 c 2

Booleans

17

Booleans

boolean data type. Useful to control logic and flow of a program.

19

Leap Year

Q. Is a given year a leap year?

A. Yes if either (i) divisible by 400 or (ii) divisible by 4 but not 100.

public class LeapYear { public static void main(String[] args) { int year = Integer.parseInt(args[ 0 ]); boolean isLeapYear; // divisible by 4 but not 100 isLeapYear = (year % 4 == 0 ) && (year % 100 != 0 ); // or divisible by 400 isLeapYear = isLeapYear || (year % 400 == 0 ); System.out.println(isLeapYear); } } % java LeapYear 2004 true % java LeapYear 1900 false % java LeapYear 2000 true

Type Conversion