SlideShare a Scribd company logo
Programming with Java Jussi Pohjolainen Tampere University of Applied Sciences
HELLOWORLD
HelloWorld.java public class HelloWorld { public static void main(String [] args) { System.out.println("Hello World"); } }
BOOLEAN ALGEBRA AND CONDITIONS
About Conditions Conditions are used in while and in if – sentences if(condition) do something Condition is a statement that is either true or false
AND if(it is raining AND car does not work) go to work by bus In Java, the AND is marked with && if(x >= 4 && x<=10) do something
AND A B A && B 1 1 1 1 0 0 0 1 0 0 0 0
OR if(it is raining OR car does not work) Go to work by bus In Java OR is marked with || if(x == 3 || x == 10) do something
OR A B A && B 1 1 1 1 0 1 0 1 1 0 0 0
Negation Negation turns true to false and wiseversa In Java, negation is marked with ! if(!rains) go to work by bicycly if(!(x < 3)) do something
Negation A !A 1 0 0 1
Combining Conditions if(!rains && (temperature > 20C)) Walk with your t-shirt on Demos Conditions.java BooleanAlgebra.java
PRIMITIVE TYPES
About Variables Simple calculator with pseudocode print &quot;Give number&quot; a := readInput() print &quot;Give another number&quot; b := readInput() sum := a + b print sum Variables? a, b and sum!
Declaring Variables In Java you have to declare a variable before using it Declaring? What is the variable's name? What is the variable's type? Type? What kind of information will be stored into the variable?
Declaring Variables in Pseudocode Integer age print &quot;Your age?&quot; age := readInput() print age;
Types Java has two kind of types Primitive Types int, byte, short, long, double, float, boolean, char Class Types Everything else, for example String, Scanner, Arrays, Vector, JButton, JCheckBox
Primitive Types in Java type size Example value byte 8 bit 5 short 16 bit 10000 int 32 bit 200000 long 64 bit 30000000 float 32 bit 1.1234 double 64 bit 1.23487367 boolean 1 bit (undefined) true or false char 16 bit 'a'
Declaring Variables with Java Examples int number; float weight; char mycharacter; You can declare and set the variable char mycharacter = 'a'; You can assign a different value to variable after declaring with the =
Declaring Variables with Java Declare variable only once! int x = 5; x = 10; System.out.println(x); // prints 10 This is wrong! int x = 5; int x = 10; // Variable already declared! System.out.println(x);
Final Variable Final variable is a special variable which value cannot be assigned later final double PI = 3.14; PI = 5.0; // Does not work!
Examples int age, shoeSize; boolean gender; char myCharacter = 'k'; double average = 7.7;
TYPE CASTING
Type Casting? class MyApp { public static void main(String [] args) { int  a = 5; short b = a; System.out.println(b); } } TB308POHJUS-L-2:temp pohjus$ javac MyApp.java MyApp.java:4: possible loss of precision found  : int required: short short b = a; ^ 1 error
Solution class MyApp { public static void main(String [] args) { int  a = 5; short b =  (short)  a; System.out.println(b); } }
Why? class MyApp { public static void main(String [] args) { int  a = 5; long  b = 5; int result = a * b; System.out.println(result); } } MyApp.java:5: possible loss of precision found  : long required: int int result = a * b; ^ 1 error
Why? int  a = 5; long  b = 5; int result = a * b; int * long -> long!
Example Result of different Calculations Operand Operator Operand Result int + / * -  int int long + / * -  int, short, long, byte long double + / * -  float double double + / * -  double double float + / * -  float float double + / * -  int, short, long, byte double
What is the result? double a = 5; int  b = 5; double result = a / b; double / int -> double
What is the result? int  a = 5; int  b = 5; double result = a / b; int / int -> int !!!
Solution int  a = 5; int  b = 5; double result = (double) a / b;
VISIBILITY OF VARIABLES
What is the problem? import java.util.Scanner; class MyApp { public static void main(String [] args) { Scanner input = new Scanner(System.in); int inputVariable; inputVariable = input.nextInt(); if(inputVariable == 7) { int myVariable = 80; } System.out.println(myVariable);  } } TB308POHJUS-L-2:temp pohjus$ javac MyApp.java MyApp.java:15: cannot find symbol symbol  : variable myVariable location: class MyApp System.out.println(myVariable); ^ 1 error TB308POHJUS-L-2:temp pohjus$
Braces and Variable visibility Variable is visible in it's section(braces) and it's child sections Variable is not visible outside of it's section. This works: int a = 1; if(true) { System.out.println(a); } This does not:   if(true) { int b = 1; } System.out.println(b);
Braces? If control statement (if, while, for) contains only one statement you do NOT have to use braces if(something) doSomething
CLASS - TYPES
Types Java has two kind of types Primitive Types int, byte, short, long, double, float, boolean, char Class Types Everything else, for example String, Scanner, Arrays, Vector, JButton, JCheckBox
Differences Primitive Type first letter lowercase int Initialized with value int x = 0; Does not have methods Class Type first letter uppercase Scanner Initialized with new Scanner x = new Scanner(); Does have methods x.nextInt();
About String String is a class type with lot of exceptions compared to other class types. Usually class types are initialized with new. In String you can initialize also with value String example = new String(&quot;Hello World&quot;); String example = &quot;Hello World&quot;;
Class – type: String String m = &quot;hello&quot;; System.out.println(m); int length = m.length(); String newVariable = m + &quot; world&quot;; System.out.println(newVariable);
Special Characters \t = tabulator \n = enter \&quot; = &quot; \' = '
INPUT AND OUTPUT JAVA
Output System.out  is a stream that normally outputs the data you write to the console Has different methods:  print, without enter println, with enter Usage System.out.println(&quot;Hello World!&quot;); System.out.print(5); System.out.println('a');
Input System.in  is an stream connected to keyboard input of console programs Problem with System.in is that it can only read one byte at a time from the console. If you want to read for example whole line of text, you have to use other classes..
Scanner and System.in Scanner – class and System.in provides easy access to keyboard input You need to import the Scanner import java.util.Scanner; You have to define to Scanner, what stream to be used when reading Scanner sc = new Scanner(System.in); After creating the Scanner, you can read user input: int i = sc.nextInt();
The use of Scanner import java.util.Scanner; public class ScannerDemo { public static void main(String [] args) { Scanner scanner = new Scanner(System.in); String name; int age; System.out.println(&quot;Your name: &quot;); name = scanner.nextLine(); System.out.println(&quot;Your age: &quot;); age = scanner.nextInt(); System.out.println(&quot;Your name is &quot; + name); System.out.println(&quot;Your age is &quot; + age);  } }
Scanner methods Scanner reader = new Scanner(System.in); int i = reader.nextInt(); double d = reader.nextDouble(); boolean b = reader.nextBoolean(); String line = reader.nextLine();
COMMENTING CODE
Commenting code Comments in code are intended for other programmers Three different kind of comments One liner Multiple lines Javadoc
Example /* This is my beautiful hello world application. Made by Jussi */ class MyApp { public static void main(String [] args) { // This prints Hello World System.out.println(&quot;Hello World!&quot;); } }
Javadoc Javadoc is a tool for creating documentation from comments. Javadoc comments start with /** and the comments may have special attributes
Javadoc Example /** * Class that provides functionality for printing * the &quot;Hello World&quot; String to the console. * * @author Jussi Pohjolainen * @version 2009-10-26 */ public class MyApp { /** * Starting point for the app * * @param args command line arguments */ public static void main(String [] args) { // This prints Hello World System.out.println(&quot;Hello World!&quot;); } }
Result
More Examples Javadoc slides http://home.tamk.fi/~pohjus/java/lectures/javadoc.html Java ME Project Works http://koti.tamk.fi/~t4hheina/mobiili1/ http://koti.tamk.fi/~c5msalo/scorchedtamk/ http://koti.tamk.fi/~c6tkoris/mobile/project/ http://koti.tamk.fi/~c7msorvo/TsunamiGame/index.html
IF, SWITCH, WHILE, DO-WHILE, FOR
If if(something) { doSomething; }
if else if(something) { doSomething; } else { doSomethingElse; }
if else if if(something1) { doSomething1; } else if(something2) { doSomething2; }
if else if else if(something1) { doSomething1; } else if(something2) { doSomething2; } else { doSomething3; }
if else if else if else if(something1) { doSomething1; } else if(something2) { doSomething2; } else if(something3) { doSomething3 } else { doSomething4; }
Intro to Switch Case int a = 1; if(a == 1) { System.out.println(&quot;you gave one&quot;); } else if(a == 2) { System.out.println(&quot;you gave two&quot;); }
Switch Case (same than previous) switch(a) { case 1: System.out.println(&quot;you gave one&quot;); break; case 2: System.out.println(&quot;you gave two&quot;); break; }
Switch Case switch(a) { case 1: System.out.println(&quot;you gave one&quot;); break; case 2: System.out.println(&quot;you gave two&quot;); break; default: System.out.println(&quot;You did NOT give 1 or 2&quot;); }
Switch Case switch(a) { case 1: case 2: System.out.println(&quot;you gave one or two&quot;); break; default: System.out.println(&quot;You did NOT give 1 or 2&quot;); }
while int i = 0; while(i < 5) { System.out.println(i); i = i + 1; }
while int i = 5; while(i >= 0) { System.out.println(i); i = i - 1; }
while to for int i = 5; while(i >= 0) { System.out.println(i); i = i - 1; } => for(int i=5; i>=0; i = i – 1) { System.out.println(i); }
Incremental i = i + 1; i++; i = i – 1; i--; i = i + 2; i += 2; i = i – 2; i -= 2;
while to for for(int i=0; i<5; i++) { System.out.println(i); }
do-while int i = 0; do { System.out.println(&quot;Hello&quot;); i++; } while(i < 3);
EXAMPLES

More Related Content

What's hot (18)

Tutorial java
Tutorial javaTutorial java
Tutorial java
Abdul Aziz
 
Understanding F# Workflows
Understanding F# WorkflowsUnderstanding F# Workflows
Understanding F# Workflows
mdlm
 
Java generics final
Java generics finalJava generics final
Java generics final
Akshay Chaudhari
 
Java Generics
Java GenericsJava Generics
Java Generics
jeslie
 
Oop2011 actor presentation_stal
Oop2011 actor presentation_stalOop2011 actor presentation_stal
Oop2011 actor presentation_stal
Michael Stal
 
Let's refine your Scala Code
Let's refine your Scala CodeLet's refine your Scala Code
Let's refine your Scala Code
Tech Triveni
 
Qcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scalaQcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scala
Michael Stal
 
Qcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharpQcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharp
Michael Stal
 
Unit I Advanced Java Programming Course
Unit I   Advanced Java Programming CourseUnit I   Advanced Java Programming Course
Unit I Advanced Java Programming Course
parveen837153
 
Functional programming
Functional programmingFunctional programming
Functional programming
Prashant Kalkar
 
Cs30 New
Cs30 NewCs30 New
Cs30 New
DSK Chakravarthy
 
Java Generics
Java GenericsJava Generics
Java Generics
Carol McDonald
 
Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...
Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...
Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...
Philip Schwarz
 
Csharp In Detail Part2
Csharp In Detail Part2Csharp In Detail Part2
Csharp In Detail Part2
Mohamed Krar
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming ii
Prashant Kalkar
 
Chapter 8 Inheritance
Chapter 8 InheritanceChapter 8 Inheritance
Chapter 8 Inheritance
OUM SAOKOSAL
 
Acciones para AmigoBot
Acciones para AmigoBotAcciones para AmigoBot
Acciones para AmigoBot
jhonsoomelol
 
Oop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalOop2010 Scala Presentation Stal
Oop2010 Scala Presentation Stal
Michael Stal
 
Understanding F# Workflows
Understanding F# WorkflowsUnderstanding F# Workflows
Understanding F# Workflows
mdlm
 
Java Generics
Java GenericsJava Generics
Java Generics
jeslie
 
Oop2011 actor presentation_stal
Oop2011 actor presentation_stalOop2011 actor presentation_stal
Oop2011 actor presentation_stal
Michael Stal
 
Let's refine your Scala Code
Let's refine your Scala CodeLet's refine your Scala Code
Let's refine your Scala Code
Tech Triveni
 
Qcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scalaQcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scala
Michael Stal
 
Qcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharpQcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharp
Michael Stal
 
Unit I Advanced Java Programming Course
Unit I   Advanced Java Programming CourseUnit I   Advanced Java Programming Course
Unit I Advanced Java Programming Course
parveen837153
 
Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...
Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...
Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...
Philip Schwarz
 
Csharp In Detail Part2
Csharp In Detail Part2Csharp In Detail Part2
Csharp In Detail Part2
Mohamed Krar
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming ii
Prashant Kalkar
 
Chapter 8 Inheritance
Chapter 8 InheritanceChapter 8 Inheritance
Chapter 8 Inheritance
OUM SAOKOSAL
 
Acciones para AmigoBot
Acciones para AmigoBotAcciones para AmigoBot
Acciones para AmigoBot
jhonsoomelol
 
Oop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalOop2010 Scala Presentation Stal
Oop2010 Scala Presentation Stal
Michael Stal
 

Viewers also liked (20)

Java Course 3: OOP
Java Course 3: OOPJava Course 3: OOP
Java Course 3: OOP
Anton Keks
 
Basics of java 2
Basics of java 2Basics of java 2
Basics of java 2
Raghu nath
 
Java Basics
Java BasicsJava Basics
Java Basics
Brandon Black
 
Java Basics
Java BasicsJava Basics
Java Basics
Rajkattamuri
 
Java basics notes
Java basics notesJava basics notes
Java basics notes
poonguzhali1826
 
PALASH SL GUPTA
PALASH SL GUPTAPALASH SL GUPTA
PALASH SL GUPTA
PALASH GUPTA
 
Introduction to basics of java
Introduction to basics of javaIntroduction to basics of java
Introduction to basics of java
vinay arora
 
Java Course 2: Basics
Java Course 2: BasicsJava Course 2: Basics
Java Course 2: Basics
Anton Keks
 
Java basics
Java basicsJava basics
Java basics
Jitender Jain
 
Java Programming for Designers
Java Programming for DesignersJava Programming for Designers
Java Programming for Designers
R. Sosa
 
2. Basics of Java
2. Basics of Java2. Basics of Java
2. Basics of Java
Nilesh Dalvi
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handling
pinkpreet_kaur
 
Java basics
Java basicsJava basics
Java basics
Hoang Nguyen
 
Core java Basics
Core java BasicsCore java Basics
Core java Basics
RAMU KOLLI
 
OOPs in Java
OOPs in JavaOOPs in Java
OOPs in Java
Ranjith Sekar
 
Java Basics
Java BasicsJava Basics
Java Basics
sunilsahu07
 
Core Java Basics
Core Java BasicsCore Java Basics
Core Java Basics
mhtspvtltd
 
Java basics part 1
Java basics part 1Java basics part 1
Java basics part 1
Kevin Rowan
 
Java Basics
Java BasicsJava Basics
Java Basics
Rkrishna Mishra
 
Ppt on java basics
Ppt on java basicsPpt on java basics
Ppt on java basics
Mavoori Soshmitha
 
Ad

Similar to Programming with Java: the Basics (20)

Java API, Exceptions and IO
Java API, Exceptions and IOJava API, Exceptions and IO
Java API, Exceptions and IO
Jussi Pohjolainen
 
Simple Java Programs
Simple Java ProgramsSimple Java Programs
Simple Java Programs
AravindSankaran
 
Simple Java Programs
Simple Java ProgramsSimple Java Programs
Simple Java Programs
AravindSankaran
 
Java Intro
Java IntroJava Intro
Java Intro
backdoor
 
Reading and writting v2
Reading and writting v2Reading and writting v2
Reading and writting v2
ASU Online
 
Fantom and Tales
Fantom and TalesFantom and Tales
Fantom and Tales
kaushik_sathupadi
 
java intro.pptx.pdf
java intro.pptx.pdfjava intro.pptx.pdf
java intro.pptx.pdf
TekobashiCarlo
 
Basic_Java_02.pptx
Basic_Java_02.pptxBasic_Java_02.pptx
Basic_Java_02.pptx
Kajal Kashyap
 
Jquery 1
Jquery 1Jquery 1
Jquery 1
Manish Kumar Singh
 
Lập trình C
Lập trình CLập trình C
Lập trình C
Viet NguyenHoang
 
SystemVerilog OOP Ovm Features Summary
SystemVerilog OOP Ovm Features SummarySystemVerilog OOP Ovm Features Summary
SystemVerilog OOP Ovm Features Summary
Amal Khailtash
 
J Unit
J UnitJ Unit
J Unit
guest333f37c3
 
Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to java
ciklum_ods
 
Ch5(loops)
Ch5(loops)Ch5(loops)
Ch5(loops)
Uğurcan Uzer
 
Java Question-Bank-Class-8.pdf
Java Question-Bank-Class-8.pdfJava Question-Bank-Class-8.pdf
Java Question-Bank-Class-8.pdf
Aditya Kumar
 
Communication between Java and Python
Communication between Java and PythonCommunication between Java and Python
Communication between Java and Python
Andreas Schreiber
 
Solutions manual for absolute java 5th edition by walter savitch
Solutions manual for absolute java 5th edition by walter savitchSolutions manual for absolute java 5th edition by walter savitch
Solutions manual for absolute java 5th edition by walter savitch
Albern9271
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
Vijay A Raj
 
Java tutorial PPT
Java tutorial PPTJava tutorial PPT
Java tutorial PPT
Intelligo Technologies
 
Java tutorial PPT
Java tutorial  PPTJava tutorial  PPT
Java tutorial PPT
Intelligo Technologies
 
Ad

More from Jussi Pohjolainen (20)

Moved to Speakerdeck
Moved to SpeakerdeckMoved to Speakerdeck
Moved to Speakerdeck
Jussi Pohjolainen
 
Java Web Services
Java Web ServicesJava Web Services
Java Web Services
Jussi Pohjolainen
 
Box2D and libGDX
Box2D and libGDXBox2D and libGDX
Box2D and libGDX
Jussi Pohjolainen
 
libGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and PreferenceslibGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and Preferences
Jussi Pohjolainen
 
libGDX: Tiled Maps
libGDX: Tiled MapslibGDX: Tiled Maps
libGDX: Tiled Maps
Jussi Pohjolainen
 
libGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame AnimationlibGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame Animation
Jussi Pohjolainen
 
Intro to Building Android Games using libGDX
Intro to Building Android Games using libGDXIntro to Building Android Games using libGDX
Intro to Building Android Games using libGDX
Jussi Pohjolainen
 
Advanced JavaScript Development
Advanced JavaScript DevelopmentAdvanced JavaScript Development
Advanced JavaScript Development
Jussi Pohjolainen
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
Jussi Pohjolainen
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
Jussi Pohjolainen
 
libGDX: Scene2D
libGDX: Scene2DlibGDX: Scene2D
libGDX: Scene2D
Jussi Pohjolainen
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame Animation
Jussi Pohjolainen
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame Animation
Jussi Pohjolainen
 
libGDX: User Input
libGDX: User InputlibGDX: User Input
libGDX: User Input
Jussi Pohjolainen
 
Implementing a Simple Game using libGDX
Implementing a Simple Game using libGDXImplementing a Simple Game using libGDX
Implementing a Simple Game using libGDX
Jussi Pohjolainen
 
Building Android games using LibGDX
Building Android games using LibGDXBuilding Android games using LibGDX
Building Android games using LibGDX
Jussi Pohjolainen
 
Android Threading
Android ThreadingAndroid Threading
Android Threading
Jussi Pohjolainen
 
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and GesturesCreating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Jussi Pohjolainen
 
Creating Games for Asha - platform
Creating Games for Asha - platformCreating Games for Asha - platform
Creating Games for Asha - platform
Jussi Pohjolainen
 
Intro to Asha UI
Intro to Asha UIIntro to Asha UI
Intro to Asha UI
Jussi Pohjolainen
 
libGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and PreferenceslibGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and Preferences
Jussi Pohjolainen
 
libGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame AnimationlibGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame Animation
Jussi Pohjolainen
 
Intro to Building Android Games using libGDX
Intro to Building Android Games using libGDXIntro to Building Android Games using libGDX
Intro to Building Android Games using libGDX
Jussi Pohjolainen
 
Advanced JavaScript Development
Advanced JavaScript DevelopmentAdvanced JavaScript Development
Advanced JavaScript Development
Jussi Pohjolainen
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame Animation
Jussi Pohjolainen
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame Animation
Jussi Pohjolainen
 
Implementing a Simple Game using libGDX
Implementing a Simple Game using libGDXImplementing a Simple Game using libGDX
Implementing a Simple Game using libGDX
Jussi Pohjolainen
 
Building Android games using LibGDX
Building Android games using LibGDXBuilding Android games using LibGDX
Building Android games using LibGDX
Jussi Pohjolainen
 
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and GesturesCreating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Jussi Pohjolainen
 
Creating Games for Asha - platform
Creating Games for Asha - platformCreating Games for Asha - platform
Creating Games for Asha - platform
Jussi Pohjolainen
 

Recently uploaded (20)

Nice Dream.pdf /
Nice Dream.pdf                              /Nice Dream.pdf                              /
Nice Dream.pdf /
ErinUsher3
 
Optimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptxOptimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptx
UrmiPrajapati3
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptxWhat is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdfAllomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
IDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptxIDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptx
ArneeAgligar
 
Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
National Information Standards Organization (NISO)
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 EmployeeHow to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
Hemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptxHemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptx
Arshad Shaikh
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
Adam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational PsychologyAdam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational Psychology
Prachi Shah
 
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptxPests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Arshad Shaikh
 
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptxDiptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Arshad Shaikh
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptxCapitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Celine George
 
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptxRai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
GeorgeDiamandis11
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle SchoolExploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
Quiz Club of PSG College of Arts & Science
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptxFinal Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 
Nice Dream.pdf /
Nice Dream.pdf                              /Nice Dream.pdf                              /
Nice Dream.pdf /
ErinUsher3
 
Optimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptxOptimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptx
UrmiPrajapati3
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptxWhat is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdfAllomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
IDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptxIDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptx
ArneeAgligar
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 EmployeeHow to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
Hemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptxHemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptx
Arshad Shaikh
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
Adam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational PsychologyAdam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational Psychology
Prachi Shah
 
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptxPests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Arshad Shaikh
 
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptxDiptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Arshad Shaikh
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptxCapitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Celine George
 
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
GeorgeDiamandis11
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle SchoolExploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptxFinal Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 

Programming with Java: the Basics

  • 1. Programming with Java Jussi Pohjolainen Tampere University of Applied Sciences
  • 3. HelloWorld.java public class HelloWorld { public static void main(String [] args) { System.out.println(&quot;Hello World&quot;); } }
  • 4. BOOLEAN ALGEBRA AND CONDITIONS
  • 5. About Conditions Conditions are used in while and in if – sentences if(condition) do something Condition is a statement that is either true or false
  • 6. AND if(it is raining AND car does not work) go to work by bus In Java, the AND is marked with && if(x >= 4 && x<=10) do something
  • 7. AND A B A && B 1 1 1 1 0 0 0 1 0 0 0 0
  • 8. OR if(it is raining OR car does not work) Go to work by bus In Java OR is marked with || if(x == 3 || x == 10) do something
  • 9. OR A B A && B 1 1 1 1 0 1 0 1 1 0 0 0
  • 10. Negation Negation turns true to false and wiseversa In Java, negation is marked with ! if(!rains) go to work by bicycly if(!(x < 3)) do something
  • 11. Negation A !A 1 0 0 1
  • 12. Combining Conditions if(!rains && (temperature > 20C)) Walk with your t-shirt on Demos Conditions.java BooleanAlgebra.java
  • 14. About Variables Simple calculator with pseudocode print &quot;Give number&quot; a := readInput() print &quot;Give another number&quot; b := readInput() sum := a + b print sum Variables? a, b and sum!
  • 15. Declaring Variables In Java you have to declare a variable before using it Declaring? What is the variable's name? What is the variable's type? Type? What kind of information will be stored into the variable?
  • 16. Declaring Variables in Pseudocode Integer age print &quot;Your age?&quot; age := readInput() print age;
  • 17. Types Java has two kind of types Primitive Types int, byte, short, long, double, float, boolean, char Class Types Everything else, for example String, Scanner, Arrays, Vector, JButton, JCheckBox
  • 18. Primitive Types in Java type size Example value byte 8 bit 5 short 16 bit 10000 int 32 bit 200000 long 64 bit 30000000 float 32 bit 1.1234 double 64 bit 1.23487367 boolean 1 bit (undefined) true or false char 16 bit 'a'
  • 19. Declaring Variables with Java Examples int number; float weight; char mycharacter; You can declare and set the variable char mycharacter = 'a'; You can assign a different value to variable after declaring with the =
  • 20. Declaring Variables with Java Declare variable only once! int x = 5; x = 10; System.out.println(x); // prints 10 This is wrong! int x = 5; int x = 10; // Variable already declared! System.out.println(x);
  • 21. Final Variable Final variable is a special variable which value cannot be assigned later final double PI = 3.14; PI = 5.0; // Does not work!
  • 22. Examples int age, shoeSize; boolean gender; char myCharacter = 'k'; double average = 7.7;
  • 24. Type Casting? class MyApp { public static void main(String [] args) { int a = 5; short b = a; System.out.println(b); } } TB308POHJUS-L-2:temp pohjus$ javac MyApp.java MyApp.java:4: possible loss of precision found : int required: short short b = a; ^ 1 error
  • 25. Solution class MyApp { public static void main(String [] args) { int a = 5; short b = (short) a; System.out.println(b); } }
  • 26. Why? class MyApp { public static void main(String [] args) { int a = 5; long b = 5; int result = a * b; System.out.println(result); } } MyApp.java:5: possible loss of precision found : long required: int int result = a * b; ^ 1 error
  • 27. Why? int a = 5; long b = 5; int result = a * b; int * long -> long!
  • 28. Example Result of different Calculations Operand Operator Operand Result int + / * - int int long + / * - int, short, long, byte long double + / * - float double double + / * - double double float + / * - float float double + / * - int, short, long, byte double
  • 29. What is the result? double a = 5; int b = 5; double result = a / b; double / int -> double
  • 30. What is the result? int a = 5; int b = 5; double result = a / b; int / int -> int !!!
  • 31. Solution int a = 5; int b = 5; double result = (double) a / b;
  • 33. What is the problem? import java.util.Scanner; class MyApp { public static void main(String [] args) { Scanner input = new Scanner(System.in); int inputVariable; inputVariable = input.nextInt(); if(inputVariable == 7) { int myVariable = 80; } System.out.println(myVariable); } } TB308POHJUS-L-2:temp pohjus$ javac MyApp.java MyApp.java:15: cannot find symbol symbol : variable myVariable location: class MyApp System.out.println(myVariable); ^ 1 error TB308POHJUS-L-2:temp pohjus$
  • 34. Braces and Variable visibility Variable is visible in it's section(braces) and it's child sections Variable is not visible outside of it's section. This works: int a = 1; if(true) { System.out.println(a); } This does not: if(true) { int b = 1; } System.out.println(b);
  • 35. Braces? If control statement (if, while, for) contains only one statement you do NOT have to use braces if(something) doSomething
  • 37. Types Java has two kind of types Primitive Types int, byte, short, long, double, float, boolean, char Class Types Everything else, for example String, Scanner, Arrays, Vector, JButton, JCheckBox
  • 38. Differences Primitive Type first letter lowercase int Initialized with value int x = 0; Does not have methods Class Type first letter uppercase Scanner Initialized with new Scanner x = new Scanner(); Does have methods x.nextInt();
  • 39. About String String is a class type with lot of exceptions compared to other class types. Usually class types are initialized with new. In String you can initialize also with value String example = new String(&quot;Hello World&quot;); String example = &quot;Hello World&quot;;
  • 40. Class – type: String String m = &quot;hello&quot;; System.out.println(m); int length = m.length(); String newVariable = m + &quot; world&quot;; System.out.println(newVariable);
  • 41. Special Characters \t = tabulator \n = enter \&quot; = &quot; \' = '
  • 43. Output System.out is a stream that normally outputs the data you write to the console Has different methods: print, without enter println, with enter Usage System.out.println(&quot;Hello World!&quot;); System.out.print(5); System.out.println('a');
  • 44. Input System.in is an stream connected to keyboard input of console programs Problem with System.in is that it can only read one byte at a time from the console. If you want to read for example whole line of text, you have to use other classes..
  • 45. Scanner and System.in Scanner – class and System.in provides easy access to keyboard input You need to import the Scanner import java.util.Scanner; You have to define to Scanner, what stream to be used when reading Scanner sc = new Scanner(System.in); After creating the Scanner, you can read user input: int i = sc.nextInt();
  • 46. The use of Scanner import java.util.Scanner; public class ScannerDemo { public static void main(String [] args) { Scanner scanner = new Scanner(System.in); String name; int age; System.out.println(&quot;Your name: &quot;); name = scanner.nextLine(); System.out.println(&quot;Your age: &quot;); age = scanner.nextInt(); System.out.println(&quot;Your name is &quot; + name); System.out.println(&quot;Your age is &quot; + age); } }
  • 47. Scanner methods Scanner reader = new Scanner(System.in); int i = reader.nextInt(); double d = reader.nextDouble(); boolean b = reader.nextBoolean(); String line = reader.nextLine();
  • 49. Commenting code Comments in code are intended for other programmers Three different kind of comments One liner Multiple lines Javadoc
  • 50. Example /* This is my beautiful hello world application. Made by Jussi */ class MyApp { public static void main(String [] args) { // This prints Hello World System.out.println(&quot;Hello World!&quot;); } }
  • 51. Javadoc Javadoc is a tool for creating documentation from comments. Javadoc comments start with /** and the comments may have special attributes
  • 52. Javadoc Example /** * Class that provides functionality for printing * the &quot;Hello World&quot; String to the console. * * @author Jussi Pohjolainen * @version 2009-10-26 */ public class MyApp { /** * Starting point for the app * * @param args command line arguments */ public static void main(String [] args) { // This prints Hello World System.out.println(&quot;Hello World!&quot;); } }
  • 54. More Examples Javadoc slides http://home.tamk.fi/~pohjus/java/lectures/javadoc.html Java ME Project Works http://koti.tamk.fi/~t4hheina/mobiili1/ http://koti.tamk.fi/~c5msalo/scorchedtamk/ http://koti.tamk.fi/~c6tkoris/mobile/project/ http://koti.tamk.fi/~c7msorvo/TsunamiGame/index.html
  • 55. IF, SWITCH, WHILE, DO-WHILE, FOR
  • 56. If if(something) { doSomething; }
  • 57. if else if(something) { doSomething; } else { doSomethingElse; }
  • 58. if else if if(something1) { doSomething1; } else if(something2) { doSomething2; }
  • 59. if else if else if(something1) { doSomething1; } else if(something2) { doSomething2; } else { doSomething3; }
  • 60. if else if else if else if(something1) { doSomething1; } else if(something2) { doSomething2; } else if(something3) { doSomething3 } else { doSomething4; }
  • 61. Intro to Switch Case int a = 1; if(a == 1) { System.out.println(&quot;you gave one&quot;); } else if(a == 2) { System.out.println(&quot;you gave two&quot;); }
  • 62. Switch Case (same than previous) switch(a) { case 1: System.out.println(&quot;you gave one&quot;); break; case 2: System.out.println(&quot;you gave two&quot;); break; }
  • 63. Switch Case switch(a) { case 1: System.out.println(&quot;you gave one&quot;); break; case 2: System.out.println(&quot;you gave two&quot;); break; default: System.out.println(&quot;You did NOT give 1 or 2&quot;); }
  • 64. Switch Case switch(a) { case 1: case 2: System.out.println(&quot;you gave one or two&quot;); break; default: System.out.println(&quot;You did NOT give 1 or 2&quot;); }
  • 65. while int i = 0; while(i < 5) { System.out.println(i); i = i + 1; }
  • 66. while int i = 5; while(i >= 0) { System.out.println(i); i = i - 1; }
  • 67. while to for int i = 5; while(i >= 0) { System.out.println(i); i = i - 1; } => for(int i=5; i>=0; i = i – 1) { System.out.println(i); }
  • 68. Incremental i = i + 1; i++; i = i – 1; i--; i = i + 2; i += 2; i = i – 2; i -= 2;
  • 69. while to for for(int i=0; i<5; i++) { System.out.println(i); }
  • 70. do-while int i = 0; do { System.out.println(&quot;Hello&quot;); i++; } while(i < 3);