Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Java: Building Blocks
       Cate Huston
       @kittenthebad
What We’ll Cover
•   Java: an Object Oriented Language

•   The Eclipse IDE

•   Writing your first program

•   Primitive ...
Java: an Object
Oriented Language
What Does Object
Oriented Mean?
• If Ikea were made of code, it would totally
   be written in an Object Oriented language...
OK, Give Me an
Example!
•   Imagine a bike. If we wanted to “code” a bike, it
    would be a lot easier if we split it dow...
Another?
•   How about a ToDo list?

•   It’s make up of tasks.

•   Each task should have things associated with it, such...
Try It!

• Think of a complex object
• Break it down into it’s component parts
• What information does each component
  ne...
The Eclipse IDE
Eclipse is a powerful, free and open source Java IDE
(Integrated Development Environment). It has some
    very useful fea...
Let’s start by making a new project.
    File => New => Java Project
Let’s call it “Hello World”.
It’s a programming tradition.
Now we make a new “class”. Java classes
are where we represent our “objects”.
Let’s call this “HelloWorld”. Notice how
 there are no spaces? That’s important.
Our first class!
It should look like this.
Writing Your First
    Program
Finally! Write Some
Code

• For our first program, we’re going to write
  something that prints out “Hello World” in
  the ...
Click on “Run” (the green “play
button), and see what happens.
What Does It All Mean?
•   public class HelloWorld {

    •   Declares our class/object with name “HelloWorld”. Everything...
Primitive Types
Building Blocks
• Primitives are the most basic kinds of “type”
  in Java (a building block!)
• You can also think of them...
For Example...
• Whole numbers, like 42, or 926 are of type
  int (or short, or long).

• Decimal numbers, like 2.34376 or...
Declaring Variables
•   We can declare a variable of a primitive type in Java as
    follows:

    •   int i = 42;

    • ...
See how we can use the “+” sign to
     include it in our output?
Strings
Strings

•   A String is not a primitive, it’s an Object,
    but we can declare it like a primitive.

•   It’s a little m...
Conditions
Comparing Things
•   We compare things in          •   Equals: ==
    Java using conditional
    logic.                   ...
Try this for different values of a
              and b
Loops
Repeating Things
• Loops are helpful for sections of code
  that we want to repeat.
• There are three kinds of loop.
 • wh...
For Loops
• For when we know how many times
  we want to repeat something.
• 10 times
 • for(int i = 0; i < 10; i++)
• For...
Repeat Something 10 Times
For each character in a string. s.charAt(i) gets
   the character in the string at position i.
While and Do-While
Loops
•   When we want to repeat something until a
    condition changes.

•   In a while loop we check...
Example While Loop
Example Do-While
Whitespace
Tidy Code
•   Tidy code is much easier to read (and debug!)

    •   Debug - fix when it’s not working.

•   As a rule, ind...
Finally...
Finally

• This slide deck covers the very basics of
  Java - the building blocks.
• It’s important to understand these, b...
@kittenthebad
http://kittenthebad.wordpress.com/

  catehuston@googlewave.com
Upcoming SlideShare
Loading in …5
×

Java Building Blocks

7,243 views

Published on

Covers the very basics of Java: writing a hello world program, primitives, strings, if statements, loops.

Published in: Education, Technology
  • Great projects with clear instructions. I've gotten an enormous feeling of accomplishment pride from making my own bookshelves and side tables. Thanks Ted! ●●● https://t.cn/A62Ygslz
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Get Instant Access to 12000 SHED PLANS, Download plans now. ☺☺☺ https://url.cn/B7NWTgCr
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • I should try it sometime on Android. Hope it will be as much helpful as these useful source HelpWriting.net This service will write as best as they can. So you do not need to waste the time on rewritings.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Sex in your area is here: ♥♥♥ http://bit.ly/369VOVb ♥♥♥
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Follow the link, new dating source: ❤❤❤ http://bit.ly/369VOVb ❤❤❤
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Java Building Blocks

  1. 1. Java: Building Blocks Cate Huston @kittenthebad
  2. 2. What We’ll Cover • Java: an Object Oriented Language • The Eclipse IDE • Writing your first program • Primitive Types • Strings • Conditions • Loops
  3. 3. Java: an Object Oriented Language
  4. 4. What Does Object Oriented Mean? • If Ikea were made of code, it would totally be written in an Object Oriented language. • Object-Oriented means that we break our code down into components (objects) with properties (fields), that can be used to make other objects, or interact with each other. • (See? It’s a little bit like Ikea furniture!)
  5. 5. OK, Give Me an Example! • Imagine a bike. If we wanted to “code” a bike, it would be a lot easier if we split it down into its component parts. • wheels (x2) • breaks • seat • frame • peddles...
  6. 6. Another? • How about a ToDo list? • It’s make up of tasks. • Each task should have things associated with it, such as: • It’s name • The date it’s due • The date we actually complete it • An estimate of how long it will take • The date we started it
  7. 7. Try It! • Think of a complex object • Break it down into it’s component parts • What information does each component need to know about itself?
  8. 8. The Eclipse IDE
  9. 9. Eclipse is a powerful, free and open source Java IDE (Integrated Development Environment). It has some very useful features for learning to program.
  10. 10. Let’s start by making a new project. File => New => Java Project
  11. 11. Let’s call it “Hello World”. It’s a programming tradition.
  12. 12. Now we make a new “class”. Java classes are where we represent our “objects”.
  13. 13. Let’s call this “HelloWorld”. Notice how there are no spaces? That’s important.
  14. 14. Our first class! It should look like this.
  15. 15. Writing Your First Program
  16. 16. Finally! Write Some Code • For our first program, we’re going to write something that prints out “Hello World” in the terminal. • (Sorry, programmer tradition)
  17. 17. Click on “Run” (the green “play button), and see what happens.
  18. 18. What Does It All Mean? • public class HelloWorld { • Declares our class/object with name “HelloWorld”. Everything inside the “{“ is part of the class. Public is to do with it’s visibility (don’t worry about that for now). • public static void main(String[] args) { • This is our “main” method, what’s called when we click “run”. The String[] args means we can pass arguments to it, if we want to. Everything inside the “{“ is part of the main method. • System.out.println("Hello World"); • This means - print out “Hello World” to the terminal. The “;” is important, it denotes the end of the line of code. We’re going to be using a lot of these. • } } • The first closing bracket denotes the end of the “main” method, the second the end of the HelloWorld class.
  19. 19. Primitive Types
  20. 20. Building Blocks • Primitives are the most basic kinds of “type” in Java (a building block!) • You can also think of them as like atoms in chemistry. • A “type” is where we say what kind of thing a variable is. • Objects are made up of other objects and primitives.
  21. 21. For Example... • Whole numbers, like 42, or 926 are of type int (or short, or long). • Decimal numbers, like 2.34376 or 1.203 are float, or double. • True or False are boolean. • A character like ‘a’ or ‘c’ is a char. Notice the single quotes? Those are important.
  22. 22. Declaring Variables • We can declare a variable of a primitive type in Java as follows: • int i = 42; • double d = 735.27; • boolean b = true; • char c = ‘h’; • So: • type variable_name = value;
  23. 23. See how we can use the “+” sign to include it in our output?
  24. 24. Strings
  25. 25. Strings • A String is not a primitive, it’s an Object, but we can declare it like a primitive. • It’s a little more complex to declare Objects (but we’ll look at that later) • String s = “hello world”; • String s = “hello world” + “nhow are you?”
  26. 26. Conditions
  27. 27. Comparing Things • We compare things in • Equals: == Java using conditional logic. • Greater Than: > • We can put this in an “if • Less Than: < statement” • Greater Than or • if (a == b ) { ... } Equal To: >= • else if (a < b) { ... } • Less Than or Equal To: <= • else { ... }
  28. 28. Try this for different values of a and b
  29. 29. Loops
  30. 30. Repeating Things • Loops are helpful for sections of code that we want to repeat. • There are three kinds of loop. • while • do while • for
  31. 31. For Loops • For when we know how many times we want to repeat something. • 10 times • for(int i = 0; i < 10; i++) • For each character in a string • for(int i = 0; i < stringname.length(); i++)
  32. 32. Repeat Something 10 Times
  33. 33. For each character in a string. s.charAt(i) gets the character in the string at position i.
  34. 34. While and Do-While Loops • When we want to repeat something until a condition changes. • In a while loop we check that condition at the start of the loop • while(a == b) { ... } • In a do-while loop we check that condition at the end of the loop. • do { ... } while (a == b)
  35. 35. Example While Loop
  36. 36. Example Do-While
  37. 37. Whitespace
  38. 38. Tidy Code • Tidy code is much easier to read (and debug!) • Debug - fix when it’s not working. • As a rule, indent in one inside each set of {}. • In longer sections of code, we can use // to denote a comment. • A comment is code that is ignored by the compiler. • The Java compiler ignores whitespace, so use line breaks wherever you think it will make your code clearer.
  39. 39. Finally...
  40. 40. Finally • This slide deck covers the very basics of Java - the building blocks. • It’s important to understand these, because everything else builds upon them. • Next, we’re going to look at Processing.
  41. 41. @kittenthebad http://kittenthebad.wordpress.com/ catehuston@googlewave.com

×