SlideShare a Scribd company logo
Java voor BioInformatica  1
Overview Agenda The eight primitive types, especially  int  and  double Declaring the types of variables Operations on primitives The assignment statement How to print results
Primitives Primitives are the “basic” data values There are eight  types  of primitives: boolean   -- used for  true  and  false  values char   -- used for single characters (letters, etc.) byte ,  short ,  int ,  long  --  four different kinds of integer (whole number) values float ,  double   -- two different kinds of decimal numbers (numbers with a decimal point) Only 8 non-objects in Java
byte short int long float double char boolean
(1) int The most important integer type is  int An  int  is a “whole” number (no decimal point) Numbers occupy memory in the computer Larger numeric types require more memory byte : 1 byte  short : 2 bytes  int : 4 bytes  long : 8 bytes An  int  can be between about two billion (two thousand million) and negative two billion If you just write a number, such as  25 , Java assumes it is an  int Hence it is easier to work with  int  values than with the other integer types ( byte ,  short , and  long ) Use  int  in preference to other integer types
(2 and 3) byte  and  short A  byte  can be between  -128  and  127 A  short  can be  -32768  to  32767 Why these numbers? These are “round numbers” in binary; for example, 0111 1111 1111 1111   is binary for 32767 1000 0000 0000 0000   is binary for -32768 The first bit is the  sign bit : a  1  means it’s a negative number Use  byte  or  short   only when You  know  the numbers are all small There are millions of numbers to remember Extra syntax is needed (will be discussed later)
(4) long long  integers are for when two billion isn’t large enough for your needs A  long  can be as long as about 19 digits A  long  occupies twice as much space as an  int Arithmetic on  long  values is slower Use  long   only when you need  really big  numbers Extra syntax is needed (will be discussed later) Even larger numbers are available in Java-- but they are objects, not primitives
(5) float float  is the other kind of “real,” or “floating point” number float  has about 8 digits of accuracy Arithmetic with  float  is  not  faster Use  float   only  to save space when there are millions of numbers involved Extra syntax is needed (will be discussed later)
(6) double A   double  represents a “real” number Also sometimes called “floating point” These are numbers with a decimal point A   double  has about 15 digits of accuracy If you just write a real number, such as  1.37 , Java assumes it is a  double Hence it is easier to work with  double  values than with  float  values Use  double  in preference to  float
(7) char Exactly one and only one character
(8) boolean true or false
An aside: approximations Integers are precise, but real numbers are always  approximate (inaccurate) Computers always use the  binary system  internally Many numbers that can be expressed precisely in decimal cannot be represented precisely in binary For example, the numbers   1.1 ,  1.2 ,  1.3 , and  1.4   can only be approximated in binary Two numbers that look the same may actually be subtly different Never test floating point numbers for equality! Only test for larger or smaller, or for “not larger” or “not smaller” This is not a  Java  rule—it’s a  programming  rule
Giving names to numbers Sometimes you know what a number is You have  10  fingers There are  24  hours in a day π is  3.141592653589793238 Numbers written like this are called  literals You can use literals any place in Java that you can use a number Sometimes you need to use names instead: classSize ,   myBankBalance ,   myAge ,   speedometerReading Names like this are called  variables The value of a variable may change Sometimes names are simply more convenient, for example, Math.PI  instead of  3.141592653589793238
Variables Before you use a variable, you must  declare  it (tell Java what  type  it is:  int ,  double ,  char , ...)  There are two reasons for this: Different types require different amounts of space So Java can prevent you from doing something meaningless (adding 5 to someone’s name, or multiplying two dates together) Before you use a variable, you must also  define  it (tell Java what value it has) It makes no sense to print out your  bankBalance , or to add  100.00  to your  bankBalance , if you don’t have a meaningful, well-defined  initial value  for  bankBalance   to start with You might assign an initial value to your variable, or compute a value, or read a value in; but you have to get one somehow
Declaring variables You  declare  variables like this: int classSize; double myBankBalance; When you declare a variable to be a primitive type, Java automatically finds space for it The amount of space Java needs to find depends on the type of the variable Think of a variable as a specially shaped “box,” designed to hold a value of a particular type An  int  variable is four bytes long and there’s a special place for the sign bit A  float  variable is also four bytes long, but the bits are used differently--some are used to tell where the decimal point goes
Giving values to variables A  variable  is just a name for some value You have to supply the actual value somehow Java  tries  to prevent you from using a variable that you haven’t given a value You can  assign  values like this: classSize = 57; myBankBalance = 123.01;  // no "$"!
Initializing variables You can give a variable an initial value when you declare it: int classSize = 30; double myBankBalance = 0.0; You can change the value of a variable many times: classSize = 57; myBankBalance = myBankBalance + 50.00;
Arithmetic Primitives have  operations  defined for them int  and  double  have many defined operations, including +   for addition -   for subtraction *   for multiplication (Old computers did not have the    character) /   for division
Order of precedence Operations with  higher precedence  are done before operations with  lower precedence Multiplication and division have higher precedence than addition and subtraction: 2 + 3 * 4   is  14 , not  20 Operations of equal precedence are done left to right: 10 - 5 - 1  is  4 , not  6
Parentheses Operations inside parentheses are done first (2 + 3) * 4  is  20 Parentheses are done from the inside out 24 / (3 * (10 - 6))  is  24 / (3 * 4)  is  24 / 12  is  2 Parentheses can be used where not needed 2 + (3 * 4)   is the same as   2 + 3 * 4 [ ]  and  { }  cannot be used as parentheses!
Assignment statements An  assignment statement  has the form: variable  =  expression  ; Examples: price = 0.69; (The  expression  can be as simple as a single literal or variable) area = pi * radius * radius; classSize = classSize + 1; This means “add one to the value in  classSize ”
Printing out results, part 1 In Java, “print” really means “display in a window on the screen” Printing on actual paper is much harder! There are two commands for printing: System.out.print( x ); Displays  x System.out.println( x ); (Pronounced “printline”) Displays  x , then goes to the next line
Printing out results, part 2 Examples: System.out.print("The sum of x and y is "); System.out.println(x + y); If  x  and  y  are both  5 , the result will be The sum of x and y is 10 If you print from an application, an output window opens automatically If you print from a browser applet, you have to open the “Java Console” window to see your output
A  python  program Here is a program, written in the Python language, to add two numbers and print out the result: PRINT 2+2
A Java program Here is the same program, written in Java: public class TwoPlusTwo {   public static void main(String args[]) {   System.out.println(2 + 2);   } }
New vocabulary primitive : one of the 8 basic kinds of values literal : an actual specified value, such as 42 variable : the name of a “box” that can hold a value type : a kind of value that a literal has or that a variable can hold declare : to specify the type of a variable
More new vocabulary operation : a way of computing a new value from other values precedence : which operations to perform first (which operations  precede  which other operations) assignment statement : a statement that associates a value with a name initialize : to assign a “starting” value
The End “ I think there is a world market for maybe five computers.” --Thomas Watson, chairman of IBM, 1943

More Related Content

PPT
M C6java7
PPT
M C6java3
PPT
Chapter 9 - Characters and Strings
PPTX
02 data types in java
PPTX
PPTX
Data Types, Variables, and Operators
DOC
Class XII Computer Science Study Material
PPT
Strings v.1.1
M C6java7
M C6java3
Chapter 9 - Characters and Strings
02 data types in java
Data Types, Variables, and Operators
Class XII Computer Science Study Material
Strings v.1.1

What's hot (19)

PDF
Oop with c++ notes unit 01 introduction
DOCX
Notes on c++
PPT
Getting started with c++
PPTX
L2 datatypes and variables
PPTX
Fundamental programming structures in java
PPSX
Data types, Variables, Expressions & Arithmetic Operators in java
PPSX
DISE - Windows Based Application Development in Java
PPT
Chapter 8 - Exceptions and Assertions Edit summary
PDF
+2 Computer Science - Volume II Notes
PPTX
Generic Programming in java
PPT
Java: Primitive Data Types
PPT
Data Handling
PPT
Generics in java
PPTX
Python-04| Fundamental data types vs immutability
PPSX
DITEC - Programming with Java
PPTX
An Introduction : Python
PPTX
Chapter 9 python fundamentals
PPTX
Python second ppt
PPT
Ap Power Point Chpt2
Oop with c++ notes unit 01 introduction
Notes on c++
Getting started with c++
L2 datatypes and variables
Fundamental programming structures in java
Data types, Variables, Expressions & Arithmetic Operators in java
DISE - Windows Based Application Development in Java
Chapter 8 - Exceptions and Assertions Edit summary
+2 Computer Science - Volume II Notes
Generic Programming in java
Java: Primitive Data Types
Data Handling
Generics in java
Python-04| Fundamental data types vs immutability
DITEC - Programming with Java
An Introduction : Python
Chapter 9 python fundamentals
Python second ppt
Ap Power Point Chpt2
Ad

Similar to M C6java2 (20)

PDF
Control structure repetition Tito Lacbayen
PPT
Java căn bản - Chapter3
PPTX
JAVA LESSON-01.pptx
PPTX
Data types in java.pptx power point of java
PDF
Lecture 2 java.pdf
PPTX
Introduction to java Programming Language
PPT
Introduction to-programming
PPT
Ch02 primitive-data-definite-loops
PPTX
Java Programming Tutorials Basic to Advanced 2
PPT
ch02-primitive-data-definite-loops.ppt
PPT
ch02-primitive-data-definite-loops.ppt
PPTX
OOP-java-variables.pptx
PPTX
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
PPTX
JAVA CLASS PPT FOR ENGINEERING STUDENTS BBBBBBBBBBBBBBBBBBB
PPT
Java Basics
PPTX
Introduction to Java Basics Programming-II.pptx
PPT
Java introduction
PPTX
Pi j1.2 variable-assignment
PPTX
Object oriented programming1 Week 1.pptx
PPTX
Java fundamentals
Control structure repetition Tito Lacbayen
Java căn bản - Chapter3
JAVA LESSON-01.pptx
Data types in java.pptx power point of java
Lecture 2 java.pdf
Introduction to java Programming Language
Introduction to-programming
Ch02 primitive-data-definite-loops
Java Programming Tutorials Basic to Advanced 2
ch02-primitive-data-definite-loops.ppt
ch02-primitive-data-definite-loops.ppt
OOP-java-variables.pptx
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
JAVA CLASS PPT FOR ENGINEERING STUDENTS BBBBBBBBBBBBBBBBBBB
Java Basics
Introduction to Java Basics Programming-II.pptx
Java introduction
Pi j1.2 variable-assignment
Object oriented programming1 Week 1.pptx
Java fundamentals
Ad

Recently uploaded (20)

PDF
Spectral efficient network and resource selection model in 5G networks
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Getting Started with Data Integration: FME Form 101
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Big Data Technologies - Introduction.pptx
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Empathic Computing: Creating Shared Understanding
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
1. Introduction to Computer Programming.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Spectral efficient network and resource selection model in 5G networks
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Getting Started with Data Integration: FME Form 101
The Rise and Fall of 3GPP – Time for a Sabbatical?
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
A comparative analysis of optical character recognition models for extracting...
Per capita expenditure prediction using model stacking based on satellite ima...
Big Data Technologies - Introduction.pptx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Empathic Computing: Creating Shared Understanding
Unlocking AI with Model Context Protocol (MCP)
MYSQL Presentation for SQL database connectivity
Diabetes mellitus diagnosis method based random forest with bat algorithm
MIND Revenue Release Quarter 2 2025 Press Release
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
1. Introduction to Computer Programming.pptx
Encapsulation_ Review paper, used for researhc scholars
Dropbox Q2 2025 Financial Results & Investor Presentation
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...

M C6java2

  • 2. Overview Agenda The eight primitive types, especially int and double Declaring the types of variables Operations on primitives The assignment statement How to print results
  • 3. Primitives Primitives are the “basic” data values There are eight types of primitives: boolean -- used for true and false values char -- used for single characters (letters, etc.) byte , short , int , long -- four different kinds of integer (whole number) values float , double -- two different kinds of decimal numbers (numbers with a decimal point) Only 8 non-objects in Java
  • 4. byte short int long float double char boolean
  • 5. (1) int The most important integer type is int An int is a “whole” number (no decimal point) Numbers occupy memory in the computer Larger numeric types require more memory byte : 1 byte short : 2 bytes int : 4 bytes long : 8 bytes An int can be between about two billion (two thousand million) and negative two billion If you just write a number, such as 25 , Java assumes it is an int Hence it is easier to work with int values than with the other integer types ( byte , short , and long ) Use int in preference to other integer types
  • 6. (2 and 3) byte and short A byte can be between -128 and 127 A short can be -32768 to 32767 Why these numbers? These are “round numbers” in binary; for example, 0111 1111 1111 1111 is binary for 32767 1000 0000 0000 0000 is binary for -32768 The first bit is the sign bit : a 1 means it’s a negative number Use byte or short only when You know the numbers are all small There are millions of numbers to remember Extra syntax is needed (will be discussed later)
  • 7. (4) long long integers are for when two billion isn’t large enough for your needs A long can be as long as about 19 digits A long occupies twice as much space as an int Arithmetic on long values is slower Use long only when you need really big numbers Extra syntax is needed (will be discussed later) Even larger numbers are available in Java-- but they are objects, not primitives
  • 8. (5) float float is the other kind of “real,” or “floating point” number float has about 8 digits of accuracy Arithmetic with float is not faster Use float only to save space when there are millions of numbers involved Extra syntax is needed (will be discussed later)
  • 9. (6) double A double represents a “real” number Also sometimes called “floating point” These are numbers with a decimal point A double has about 15 digits of accuracy If you just write a real number, such as 1.37 , Java assumes it is a double Hence it is easier to work with double values than with float values Use double in preference to float
  • 10. (7) char Exactly one and only one character
  • 11. (8) boolean true or false
  • 12. An aside: approximations Integers are precise, but real numbers are always approximate (inaccurate) Computers always use the binary system internally Many numbers that can be expressed precisely in decimal cannot be represented precisely in binary For example, the numbers 1.1 , 1.2 , 1.3 , and 1.4 can only be approximated in binary Two numbers that look the same may actually be subtly different Never test floating point numbers for equality! Only test for larger or smaller, or for “not larger” or “not smaller” This is not a Java rule—it’s a programming rule
  • 13. Giving names to numbers Sometimes you know what a number is You have 10 fingers There are 24 hours in a day π is 3.141592653589793238 Numbers written like this are called literals You can use literals any place in Java that you can use a number Sometimes you need to use names instead: classSize , myBankBalance , myAge , speedometerReading Names like this are called variables The value of a variable may change Sometimes names are simply more convenient, for example, Math.PI instead of 3.141592653589793238
  • 14. Variables Before you use a variable, you must declare it (tell Java what type it is: int , double , char , ...) There are two reasons for this: Different types require different amounts of space So Java can prevent you from doing something meaningless (adding 5 to someone’s name, or multiplying two dates together) Before you use a variable, you must also define it (tell Java what value it has) It makes no sense to print out your bankBalance , or to add 100.00 to your bankBalance , if you don’t have a meaningful, well-defined initial value for bankBalance to start with You might assign an initial value to your variable, or compute a value, or read a value in; but you have to get one somehow
  • 15. Declaring variables You declare variables like this: int classSize; double myBankBalance; When you declare a variable to be a primitive type, Java automatically finds space for it The amount of space Java needs to find depends on the type of the variable Think of a variable as a specially shaped “box,” designed to hold a value of a particular type An int variable is four bytes long and there’s a special place for the sign bit A float variable is also four bytes long, but the bits are used differently--some are used to tell where the decimal point goes
  • 16. Giving values to variables A variable is just a name for some value You have to supply the actual value somehow Java tries to prevent you from using a variable that you haven’t given a value You can assign values like this: classSize = 57; myBankBalance = 123.01; // no "$"!
  • 17. Initializing variables You can give a variable an initial value when you declare it: int classSize = 30; double myBankBalance = 0.0; You can change the value of a variable many times: classSize = 57; myBankBalance = myBankBalance + 50.00;
  • 18. Arithmetic Primitives have operations defined for them int and double have many defined operations, including + for addition - for subtraction * for multiplication (Old computers did not have the    character) / for division
  • 19. Order of precedence Operations with higher precedence are done before operations with lower precedence Multiplication and division have higher precedence than addition and subtraction: 2 + 3 * 4 is 14 , not 20 Operations of equal precedence are done left to right: 10 - 5 - 1 is 4 , not 6
  • 20. Parentheses Operations inside parentheses are done first (2 + 3) * 4 is 20 Parentheses are done from the inside out 24 / (3 * (10 - 6)) is 24 / (3 * 4) is 24 / 12 is 2 Parentheses can be used where not needed 2 + (3 * 4) is the same as 2 + 3 * 4 [ ] and { } cannot be used as parentheses!
  • 21. Assignment statements An assignment statement has the form: variable = expression ; Examples: price = 0.69; (The expression can be as simple as a single literal or variable) area = pi * radius * radius; classSize = classSize + 1; This means “add one to the value in classSize ”
  • 22. Printing out results, part 1 In Java, “print” really means “display in a window on the screen” Printing on actual paper is much harder! There are two commands for printing: System.out.print( x ); Displays x System.out.println( x ); (Pronounced “printline”) Displays x , then goes to the next line
  • 23. Printing out results, part 2 Examples: System.out.print("The sum of x and y is "); System.out.println(x + y); If x and y are both 5 , the result will be The sum of x and y is 10 If you print from an application, an output window opens automatically If you print from a browser applet, you have to open the “Java Console” window to see your output
  • 24. A python program Here is a program, written in the Python language, to add two numbers and print out the result: PRINT 2+2
  • 25. A Java program Here is the same program, written in Java: public class TwoPlusTwo { public static void main(String args[]) { System.out.println(2 + 2); } }
  • 26. New vocabulary primitive : one of the 8 basic kinds of values literal : an actual specified value, such as 42 variable : the name of a “box” that can hold a value type : a kind of value that a literal has or that a variable can hold declare : to specify the type of a variable
  • 27. More new vocabulary operation : a way of computing a new value from other values precedence : which operations to perform first (which operations precede which other operations) assignment statement : a statement that associates a value with a name initialize : to assign a “starting” value
  • 28. The End “ I think there is a world market for maybe five computers.” --Thomas Watson, chairman of IBM, 1943