![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Sexy Programmer
|
Programming with Java: Tutorial
Programming with Java
------------------------------------ "Intro to Java" First I would like to say Java is a great programming language. Its flexible and a lot can be done with it. While other high level language are translated into native code or machine code.Java is translated "Java Bytecode", which is read by the a small Java program itself called the JVM(Java Virtual Machine). The JVM is considered an interpreter. There are many reasons Javauses it's own Virtual Machine and not compile like other programming languages. One needs to understand this before going on and writing program using Java. Using Java Bytecode makes Java very popular in industries who needs a independent program to "Run Anywhere". The J.D.K (Java Development Kit) are the tools used to create and develop Java programs. You can download Java, JVM, and JDK via Sun Microsystems's Web Site. http://java.sun.com/j2se/1.5.0/download.jsp It's necessary to have the lastest JRE and JDK in order to write Java programs. In order to run any Java program on your computer, you need the JRE. Only if web sites have Java Applets embemmed. This is an type of Java program which is specially used for Web Pages. Examples of Applets are on a lot of popular web sites like www.yahoo.com and others. ------------------------------------ "Data Types" There are a range of data types in the programming language of Java. Here are just some of the data types that are available. char - a character like a,b,c, etc. String -- a group of characters or words. int -- Integer, a number ranging from -2147483648 to 2147483647. float -- a Integer but with a longer range of numbers than int, -9223372036854775808 to 9223372036854775807. single -- a number with a decimals. double -- also a number with a decimals but with a longer range. basics on which one can do what. ------------------------------------ "How to Write A Program in Java" You only need a text editor like Notepad or Vim to write Java programs. You can compile and run Java program you've written through the command line once you've installed Java and the JDK. You can also use IDE (Integrated Development Enviroment) such as Eclipse, Bluejay, and JEdit. I consider IDEs to be great. But I recommend a new user to use the command line to run and compile their programs. Its good practice and you will appreciate it more! :-)! Let's get start by opening your editor or IDE, which ever you prefer. Start by creating a new Java file also called a Java Class. Java uses the extension ".java" and it's compiled buddy's extension is ".class". Remember that what ever you name you file, that has to be the name of your main class. Now lets get into the writing part. Lets create a file called Welcome.java and inside that file we are going to write... [PHP] public class Welcome { public static void main(String args[]) { System.out.println("Welcome World!"); } } [/PHP] Now we are going to discussion what all of these words actually mean. [PHP] public class Welcome { //methods go here.... } [/PHP] It is a class. All Java program is made up of classes. The class "Welcome" is declared "public". We are not going to go into what else we can do because this is just a a short tutorial on Programming with Java. [PHP] public static void main(String args[]) { //statements of code go here... } [/PHP] This is what every Java program needs in order to run a program, A "main method". For those who have experience in other programming languages methods are just re-named functions. A method something that carries out a specific task that is wanted to be done by the programmer. The Java API is full of classes with methods just specifly designed to carry out something. Here's an example of using the Math class located in The java.lang package. [PHP] public class Welcome { public static void main(String args[]) { int x; int y; int sum; x = (int) (Math.random() * 10) + 1; y = (int) (Math.random() * 10) + 1; sum = y + x; System.out.println("The sum of x and y is " + sum); } } [/PHP] What I've used is the "Math.random()" statement to generate a random number 1 thru 10. Because the Math.random() generates a floating-number, I made it an integer number so that things can be easier. Also, Math.random() generates a number that is more or equal to zero but less than one, "0 <= Math.random() < 1". That's why it is necessary to multiply it by 10 and add 1. So that the number 1 thru 10 can be generated. Play around with the example and see what you can come up with. The "Math.random()" method's task is only to create a random number not anything more, not anything less. For this section, start creating your own methods instead of having your main method do everything else. Methods make programming a lot easier and it is easier to debug a program when a program is broken down into parts. It will not matter now when creating < 20 line program, when you want to create your own Media Player or something else that will be large, you will see it's a must to have different methods. No one can write a large program perfectly in one shot.
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#2 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3
![]() |
might I suggest the tutorial section? :p
|
|
|
|
|
|
#3 |
|
Sexy Programmer
|
yeah I didn't have the rights to post this in there so maybe Big K would do it for me.
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#4 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3
![]() |
You might to get your information correct... I read as far as data types and then cracked up so hard I stopped reading...
|
|
|
|
|
|
#5 |
|
Professional Programmer
|
Float's aren't integers, long's are (which you forgot to mention). There's no such thing as a single. You forgot short, byte and boolean. Also, String isn't a data type, it's a class.
I don't think you should be writing a tutorial quite yet. |
|
|
|
|
|
#6 |
|
Newbie
Join Date: May 2008
Posts: 2
Rep Power: 0
![]() |
Re: Programming with Java: Tutorial
q
|
|
|
|
|
|
#7 |
|
Not a user?
Join Date: Sep 2007
Posts: 256
Rep Power: 2
![]() |
Re: Programming with Java: Tutorial
I would suggest doing a spell-check and a grammar check too before submitting it.
|
|
|
|
|
|
#8 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3
![]() |
Re: Programming with Java: Tutorial
old thread
</discussion>
__________________
<insert disclaimer here> <insert shameless plug for Visual Studio here> |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|