M C6java2

386
-1

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
386
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

M C6java2

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

×