Computational Applications in Nuclear Astrophysics using Java Java course Lecture 2 Prepared for course 160410/411 Michael C. Kunkel m.kunkel@fz-juelich.de Materials taken from; docs.oracle.com Teach Yourself Java ISBN 9780078825705
Did you get this output? Homework Review
Homework Review
Java Data Types A data type specifies the type and size of values to be stored in an identifier. Java has 2 types of data type Reference data types (Non-Primitive data type) Primitive data types
Java Data Types Data Types Primitive Data Types char boolean integer floating point byte double int float long short Reference Data Types Arrays Classes Interfaces String etc. = Mostly used in this class
Java Primitive Data Types char data type is a 16-bit Unicode character ( u0000 to uffff ) : u0000 boolean data type can only have values "true" or "false" : false Not "0" or "1" like C ++ byte data type is a 8-bit integer (-128 to 127) : 0 short data type is a 16-bit integer ( 32768 to 32767) : 0 int data type is a 32-bit integer ( 2,147,483,648 to 2,147,483,647) : 0 long data type is a 64-bit integer (9,223,372,036,854,775,808 to 9,223,372,036,854,755,807) : 0L float data type is a 32-bit floating point (3.4e 038 to 3.4e+038) : 0.0f double data type is a 64-bit floating point (1.7e 308 to 1.7e+038) : 0.0d
Class Work Create a package in Eclipse Name it secondclass Create a public class named PrimitiveTypes Create a main method Using standard output, declare and print each type of primitive data type once. For the boolean case, try to set the value to a logical argument i.e. myboolean = (42>24)
Java Escape Sequences Escape sequences are used to signal an alternative interpretation of a series of characters. In Java, a character preceded by a backslash (\) is an escape sequence and has special meaning to the java compiler. Here are some example: \' print single quote \" print double quote \\ print a backslash \t print a tab \n print a new line
Class Work Using the package secondclass Create a public class named EscapeTypes Create a main method Using standard output, declare and print each kind of escape sequence once.
Java Escape Sequences
Class Work Using the package secondclass Create a public class name ReferenceTypes Create a main method Create another class named AType In AType declare an int, a double and a char In the main method Instantiate an object of Type. Set each primitive of AType to a value Using standard output and print each kind of escape sequence once.
Enum Type "An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it"
Enum Type Example
Class Work Using the package secondclass Create a public class name EnumTypes Create a main method Create an enum name IceCream Fill in the enum with at least 4 different ice creams In the main method Using standard output and print a sentence naming your least favourite ice cream from your enum.
When to Use What Data Type Why When How char We may need only a character value instead of using larger set of characters (Strings). We need to use only one character value. char mychar = 'a'; boolean Easy to use (can have only two values). Also has better memory usage values than some other types. We need to use simple flags that track true or false conditions. boolean mybool = true;
When to Use What Data Type Why When How byte Sometimes it can be better to use only a little part of memory than reserving/holding large parts for small processes. When memory savings actually matters. byte mybyte = 127; short Sometimes it can be better to use only a little part of memory than reserving/holding large parts for small processes. When memory savings actually matters. But when we need to use a little larger numbers. short myshort = 32767;
When to Use What Data Type Why When How int We may need to use numbers in our program. We need to represent 32-bit unsigned integer int myint = 1; long Sometimes int types can not hold larger values. We need to use larger numbers than the numbers provided by int. long mylong = 96543232934543L;
When to Use What Data Type Why When How float We may need to use numbers with decimal points. We need to use single precision 32-bit floating point numbers. float myfloat = 11.5F; double We may need to use numbers with decimal points. We need to use double precision 64-bit floating point numbers. double mydouble = 3.14; enum Enum constants cannot be changed after created. (Implicitly static and final) When you need to use predefined constant sets. enum Day {Sunday, Monday, Tuesday} Day.Monday
Predefined Java Classes Java has numerous built in classes to aide in programming in Java Some we have already used/mentioned String System One that we will use numerous times is the Math class
Class Work Using the package secondclass Create a public class name MathClassExamples Create a main method In the main method Using standard output and print at least 4 different math operations on any numeric data type. (Hint: Use Eclipse's content assist to see the different methods in the Math class)
Operators in Java As in most programming languages, operators are special symbols that perform specific operations Java has Assignment operators Logical operators Arithmetic operators Unary operators Relational and Equality operators Bitwise and Bitshift operator Comma operator Ternary operator = Homework = Will discuss today
Assignment Operators An assignment operator assigns the value on the right to the operand on the left example: int myint = 10; Operation on myint = 10 Assignment Value after Operation myint += 5 addition 15 myint -= 5 subtraction 10 myint *= 5 multiplication 50 myint /= 5 division 2 myint %= 5 modulus 0
Arithmetic Operators An arithmetic operator perform basic math operations example: int myint = 10; Operation on myint = 10 Arithmetic Value after Operation myint + 6 addition 16 myint - 6 subtraction 4 myint * 6 multiplication 60 myint / 6 division 1 myint % 6 remainder (remainder) 4
Logical Operators An logical operator returns a Boolean result that is based on the Boolean result of one or two other expressions Operation && Logic AND OR! NOT
Unary Operators The unary operators require only one operand; they perform various operations such as incrementing/ decrementing a value by one, negating an expression, or inverting the value of a boolean. Symbol Operator Description + Unary plus Indicates positive value - Unary Minus Negates an expression ++ Increment Increments a value by 1 -- Decrement Decrements a value by 1! Logical complement Inverts the value of a boolean
Relational and Equality Operators The equality and relational operators determine if one operand is greater than, less than, equal to, or not equal to another operand. Symbo Description "==" equal to (had to use "")!= not equal to > greater than >= greater than or equal to < less than <= less than or equal to
Comma Operators A comma operator is used to split up arguments within a functions' parameters.
Homework Teach yourself what Bitwise and Bitshift operators are Using the package secondclass Create a public class name SecondClassHomeworkYOURNAME Create a main method In the main method Using standard output Print at least 12 different operations using either arithmetic, logical, assignment, relational or equality. Demonstrate you can taught yourself what a ternary operator is Push your homework to GitHub
Homework Since the function of the lab is to Learn basic Java Reenforce lectures through programming The homework will be handed in electronically Via Github
Homework The homework will be graded as Does it run (boolean doesrun) Does it produce proper results (boolean iscorrect) Is it original (boolean isoriginal)
GitHub Please make sure you have git installed on your machine Most UNIX machines have it prepackage git --version Most Windows machines do not come prepackage with git I will be helping install this for those who need
GitHub Please go to https://github.com/ Make an account (its free) Got to https://github.com/mckunkel/bochumcourses Click to green link to "Clone or Download" Copy the link In a terminal make a directory where you want to put your Github repository type git clone LINKYOUCOPIED
Homework
Maven When ready to work on your homework Open Eclipse Click File->Import->Maven Project- >Existing Maven Project Click the check box for the pom file included Click Finish More on Maven in next lecture
Homework
Homework
GitHub When ready to submit your homework go to your Github folder go to the base directory with the homework is type git add * git commit -am "PUT A MESSAGE HERE" git push
Homework
Next Class More Java More on Maven Code examples from lecture 1 lecture 2