java 101

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

java 101

Post by caracarn001 on Wed Mar 16, 2011 9:35 am
([msg=55123]see java 101[/msg])

This thread is intended for those who want to learn java. I'll assume you don't have any prior programming experience.


lesson1

What is java:
Java is a platform independent, object-oriented programming language. If you are new to the programming scene, this may mean nothing to you. The gist of it is that java is used to build applications (and applets) that can be used on all operating systems ( windows, linux, osX). It uses an object-oriented approach instead of the old top-down programming (like in Pascal). Objects will not be covered in this lesson.

What do we need to make a java program
a JRE (java runtime environment): Downloadable for free on the java website.
a IDE (integrated development environment): There is more than one way to write and compile your java files. You can use netbeans which has a drag-drop system for your visual components and generates a load of code for you. Or you if you prefer to know what you're doing (like me), you can use eclipse. I'm sure there are more IDE's out there, so if neither of these work for you, search the interwebs, I'm sure there is a java-match for you.

note: I am an eclipse-user. Some things are different between IDE's. I'll try explaining what i'm doing as best as I can, but if I'm giving menu-sequences, they won't work for you.

hello world
Great, we have everything we need to start building our own applications. Start you IDE and create a new project(File->new->java project). Call it java101. In this project start a new Package(right click on the project->new->package). Call this package examples. Finally in this package create a new class(right click on the package->new->class. Call it HelloWorld.

What follows is the code of the HelloWorld-application. explication of the line can be found after the '//'
Code: Select all
package examples;                         //the package we're in

public class HelloWorld {                   //start of our class

   public static void main(String args[]) {    //start of our main-method
      System.out.println("Hello world");       //print "Hello world" in the console
   }                                 //end of our main-method
}                                    //end of our class



I know you're wondering why i called my method "public static void main(String args[])". This is where the application starts. Thus every java-application must have it (if you want to be able to run it).

variables
If you want to learn about variables in java you should be made aware that, unlike in php, java isn't loose typed. Meaning every variable is java must have a type. Luckily for us the java-language has provided us with some primitive types to work with:
  • byte: a numerical value between -128 and 127. Uses 8 bit.
  • short: a numerical value between -32,768 and 32,767. Uses 16 bit.
  • int: a numerical value between --2147483648 and 2147483647. Uses 32 bit.
  • long: a numerical value between -9223372036854775808 and a maximum value of 9223372036854775807. Uses 64 bit.
  • float: use this for floating point values. Uses 32 bit.
  • double: use this for floating point values. Uses 64 bit.
  • char: a unicode character. Uses 16 bit.
  • boolean:true or false
Finally there is the String. It isn't a primitive type, but it can be used as such.

What follows is the HelloWorld-application using variables.
Code: Select all
package examples;

public class HelloWorld {
   public static void main(String args[]) {
      String variable = "Hello world";      //declaring and initializing a variable of the type String
      System.out.println(variable);
   }
}


Declaring and initializing a variable of a primitive type is easy in java. First you say what type you want, then you name the variable. Finally you give it a value. some examples:
int i = 1;
boolean flag = true;
char value = 'a';
String text ="hey, this is easy";

As you can see, for all the mathematical values, you can put the desired number. However, if you need a char, then you need to use single quotes. Double quotes are used for Strings.

go with the flow
1: conditions
Often in programming you'll find that you want something to happen only if a condition is met. Have no fear, java has, once more, got a solution for you. The if-statement.
Code: Select all
if (condition) {
   things to do if the condition is met.
}

Then there are those times that you want to do one thing if a condition is met and something entirely else if it isn't.
Code: Select all
if (condition) {
   things to do if the condition is met.
} else {
   things to do if the condition is not met.
}


finally there are those times that there are numerous outcomes possible. For those times java has the switch statement.
Code: Select all
Switch (variable) {
   case (value1):
      things to do if (variable) = (value1);
      break;
   case (value2):
      things to do if (variable) = (value2);
      break;
   case (valueN):
      things to do if (variable) =(valueN);
      break;
   default:
      things to do if none of the previous values where correct;
}


2:loops
Sometimes we want to do a set of statements more than once. Java provides us with 3 ways to do this. The first type of loop is the for-loop. We use this loop if we know how many times we want to go through our loop.
The following code shows how to go ten times through a loop and write the value of i in the console.
Code: Select all
for(int i=0; i<10; i++) {
   System.out.println(i);
}

This is the first time we used ++. It is a simplified way of writing i=i+1. There is also i-- (i=i-1).

Alas we don't always know how many times we want to go through our loop. For those times java provides us with the while-loop and the do-while-loop. The difference between these two is that the while loops checks if a condition is met before it goes through it's first loop. The do-while-loop goes through its first loop and then checks if it has to go through it again. Therefore a do-while-loop is executed at least once.
Code: Select all
//the while loop
while(condition) {
   things to do;
}

Code: Select all
//the do-while-loop
do {
   things to do;
} while (condition);


Be aware that you should always (at some point in your loop) change the value of your condition so that it is met. Otherwise you will write an endless loop and your program will not function beyond that loop.

operators
1: assignment operator
  • assign = (int number = 12;)
2: arithmetic operators
  • plus +
  • minus –
  • multiply *
  • divide /
  • mdulo %
3: relational operators
  • bigger than >
  • smaller than <
  • bigger than or equal >=
  • smaller than or equal >=
  • equal ==
  • not equal !=
4: logical operators
  • and &&
  • or ||
  • not !
  • exclusive or ^
  • and (*) &
  • or (**) |
(*)only evaluates the second part of a statement if the first part is true
(**)only evaluates the second part of a statement if the first part is false
5: bit-wise operators

  • and &
  • or |
  • exclusive or ^
  • complement ~
  • left shift <<
  • right shift >>
  • unsigned right shift >>>


If interested I'll continue this.
If you found mistakes, send me a pm. I'll adjust and mention your name somewhere :P
User avatar
caracarn001
New User
New User
 
Posts: 35
Joined: Thu Nov 04, 2010 5:23 am
Blog: View Blog (0)


Re: java 101

Post by fashizzlepop on Wed Mar 16, 2011 12:53 pm
([msg=55127]see Re: java 101[/msg])

Why not submit this as an article or lecture?
The glass is neither half-full nor half-empty; it's merely twice as big as it needs to be.
User avatar
fashizzlepop
Moderator
Moderator
 
Posts: 2147
Joined: Sat May 24, 2008 1:20 pm
Blog: View Blog (0)


Re: java 101

Post by Goatboy on Wed Mar 16, 2011 1:07 pm
([msg=55130]see Re: java 101[/msg])

Not bad, but there is definitely room for improvement.

The first thing that jumps out at me is that this guide would confuse the hell out of most people with no programming experience. What is a function? What is a class? What are declaring and initializing? What is a floating point? What is unicode? What is an operator? I would either add way more explanation, or change the scope of the article.

Second, it would be beneficial if you could provide examples of each concept being explained. What mistakes might someone make frequently? What happens if you ++ a short that is at 32,767? When would boolean be used? What the hell would any of this code actually do? How do I run it?

Third, and this is solely to make it look better, clean up your English a bit. Specifically, there's a lot of hyphenation going on where it should not be used. Try to keep even spacing when using parens, and don't use a curly brace to open if you're not going to end with a curly brace.

Last, do what fashizzle said. This should really go into Articles, once you clean it up, of course.
Mundus Vult Decipi
User avatar
Goatboy
Expert
Expert
 
Posts: 2443
Joined: Mon Jul 07, 2008 9:35 pm
Blog: View Blog (0)


Re: java 101

Post by thetan on Wed Mar 16, 2011 1:57 pm
([msg=55134]see Re: java 101[/msg])

I've never attended a programming class of any nature. I took a semesters worth of CS classes and decided to dropout not shortly after because i was insanely bored and unimpressed by the students and the professors.

However, i didn't think it was that hard to follow. If anyone payed attention in HS math they should know the general concept of a function.

An interesting approach at the least. good job.

Insanely awkward timing too, as i just started writing an 'Intro to functional programming'. Maybe i'll finish it and post it after work.
"If art interprets our dreams, the computer executes them in the guise of programs!" - SICP

Image

“If at first, the idea is not absurd, then there is no hope for it” - Albert Einstein
User avatar
thetan
Contributor
Contributor
 
Posts: 653
Joined: Thu Dec 17, 2009 6:58 pm
Location: Various Bay Area Cities, California
Blog: View Blog (0)


Re: java 101

Post by fashizzlepop on Fri Mar 18, 2011 9:36 pm
([msg=55223]see Re: java 101[/msg])

Please do, Thetan.
The glass is neither half-full nor half-empty; it's merely twice as big as it needs to be.
User avatar
fashizzlepop
Moderator
Moderator
 
Posts: 2147
Joined: Sat May 24, 2008 1:20 pm
Blog: View Blog (0)


Re: java 101

Post by mShred on Fri Mar 18, 2011 9:40 pm
([msg=55224]see Re: java 101[/msg])

fashizzlepop wrote:Please do, Thetan.

Already done my friend. viewtopic.php?f=36&t=7008
Image

For those about to rock.
User avatar
mShred
Administrator
Administrator
 
Posts: 1477
Joined: Tue Jun 22, 2010 4:22 pm
Blog: View Blog (2)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests