PDA

Click to See Complete Forum and Search --> : basic JAVA tutorial


hot_ice
January 13th, 2002, 01:58 PM
OK, well, I really liked Mystic Ravenous's tutorial on C (me learning it now) and I thought I'd do something very similar with java, cos I'm kinda learning it now and know the basics of it.

Here goes...

This first program will be similar to Mystic's first program, I'll store some variables and then print them onto the screen - then we'll progress from there. (don't worry too much about classes and methods at the moment.)

public class Basic //in java, things always need to be in a class
{//opens class
public static void main(String[] args) //methods reside inside classes
{//opens method
String theString = "Hello"; //declaring and initializing a string (not capital S)
char theChar = 'A'; //declaring and initializing a character
int theInt = 45; //declaring and initializing a integer
double theDouble = 4.3688394; //declaring and initializing a double (there are also floats, but they are similar to doubles)

System.out.println(theString); //prints to screen the data held in 'theString' variable
System.out.println(theChar); //you get the point
System.out.println(theInt);
System.out.println(theDouble);
System.out.println("This just prints anything inside the quotes...3u73...39dhsd...yup");
System.out.println("the integer value is: " + theInt); //use + to seperate your own output and variables
}
}//closes class

ok, here is the code without the comments (note, as with C, everything after the // is ignored by the compiler - comment):

public class Basic
{
public static void main(String[] args)
{
String theString = "Hello";
char theChar = 'A';
int theInt = 45;
double theDouble = 4.3688394;

System.out.println(theString);
System.out.println(theChar);
System.out.println(theInt);
System.out.println(theDouble);
System.out.println("This just prints anything inside the quotes...3u73...39dhsd...yup");
System.out.println("the integer value is: " + theInt);
}
}

Some explanations:
1. 'public static void main(String[] args)' is the MAIN method in java that almost all programs have - except for applets...just remember that line and you'll be right - I don't even really know what all those words in it mean
2. 'System.out' this is a built-in class that comes with java and is used to display things on the screen - use '.println' after it to display data and then move to new line or use '.print' to display without moving to a new line
3. note: all data types begin with lower case except for String - so just keep this in mind.

ok, now I'm sure I probably forgot to explain something, so if I did or you just don't get something, just post a reply and I'll answer it.

I will hopefully return to post another tutorial that teaches you something else...hope this helps someone!! (thanks to Mystic Ravenous who gave me the idea to do this) :)


Greg

hot_ice
January 14th, 2002, 08:58 AM
ok, just wanted to add a few things for all people who don't know how to start.

The basics you need to write a java program (like the one above) is to have a basic text editor and the JDK (Java Development Kit) - (or SDK now I think)...either, it doesn't matter.

You can get the JDK/SDK from Sun's website:
http://java.sun.com/j2se/
then simply follow the links that apply to you

Greg

Mystic Ravenous
January 16th, 2002, 01:35 AM
Very cool, and informative, I'm glad other people are writing tutorials.. Once you gain some knwoledge distribute it. ..Help others, As of others helped you,.. :)


cheers,
MR

coolbuzz
April 1st, 2002, 11:46 AM
Not so informative IMO for a beginner

celfie
April 1st, 2002, 02:10 PM
1. 'public static void main(String[] args)' is the MAIN method in java that almost all programs have - except for applets...just remember that line and you'll be right - I don't even really know what all those words in it mean

-> Servlets do not have a main method as well. And no, many objects do not have these. For example, objects that are used solely for utility purposes. You also fail to mention what public static void means. public means that the method can be accessed from all other objects. Static means you do not need to create an instance of the object to use the method for only one instance of the method will always reside in memory. void means that the method will return nothing.String[] args is an argument. This particular one puts any command line options into an array called args. In java objects begin with a capital letter so that would really be Args. Unlike C, the Args array, begins with the first command line argument at index=0.


2. 'System.out' this is a built-in class that comes with java and is used to display things on the screen - use '.println' after it to display data and then move to new line or use '.print' to display without moving to a new line

-> System.out is an object not a class. It contains methods for output. print() and println() are methods of the object.

3. note: all data types begin with lower case except for String - so just keep this in mind.

-> all primitive data types are lower case (int, float, char, double, boolean, byte, short,long), reference types are also lower case (array, class, interface). String is uppercase because it is an object. For example String, Integer, Hashtable, Vector.

if you really are interested in learning java, go read the tutorial on java.sun.com, it is fine and you wont find much better.

owen76
August 26th, 2002, 03:36 PM
What is the best way to input data from the keyboard?

An example would be quite helpful. :)

Later_Gator
August 29th, 2002, 06:15 PM
great job.. helping people learn is always great...

Guus
August 29th, 2002, 06:22 PM
Owen, look here: http://www.csupomona.edu/~cs/labs/help_pages/docs/java/Keyboard_Input.htm

owen76
September 4th, 2002, 03:05 PM
Thanks Guus! My teacher is trying to suck us into jbuilder. I prefer the jdk but didn't have anything to help with keyboard input. I had tried Sun's site but wading through the documentation can be a chore. The site is quite helpful. :)

Guus
September 4th, 2002, 03:20 PM
Before you go to bed tonight, sit down on your knees and say a prayer to the uber-god of Google :)

nebulus200
September 4th, 2002, 03:32 PM
It would probably be helpful if you started out describing some of the basic features and terminology of java before jumping right into a program. The program might not be that hard to jump into if you are familiar with c++ and objects, but it is the kind of thing that if you haven't seen before, this could kind of blow people's minds.

I would suggest starting off describing the object oriented model. Descriping how it can take on characteristics of a real object and how objects can interact. One of the best ways I have seen this described is to right a sentence about something you want your program to do, the nouns can be your objects, the verbs/adverbs your methods. Once you get that out, I would recommend, then discussing how java is entirely object oriented, how everything in java is an object and that there are several 'sub-objects' that inherit properties of lower level objects and then expand upon them (and then in turn are inherited). This would go a long way to explaining why there is a system object and why it is used for outputing, why you are using string objects, etc.

Once you have all that down, then I would jump into a sample program like you did and demonstrate how the objects interact. Once you have down the fundamental idea of java being object oriented and basically centered around object interaction, the rest is basically learning syntax (which is very very similar to c++)...

Neb

Damn, couple of corrections, expanding of what I was saying (I tried to get this out too quick). 'described is to right' --> 'described is to write'

Also when I was discussing object interactions and inheriting object properties, and how there can be multiple levels of object property inheritance and how this leads to things in your code like System.out.println, where you are accessing the println method in the out object in the system object (or something to that effect, sorry been a few years since I used java). The other thing that I was getting at in the discussion of everything being object oriented that I left off is that java ITSELF is entirely object oriented, that every operator and data type and method is defined within various objects (ie., system.out.println) and that is why it is so critical to fully understand object oriented programming to properly understand java...


neb

zombielord
September 23rd, 2002, 06:45 PM
I will be posting my own Java Tutorial (my first tutorial) within the next day or so. Keep your eyes open.

Viperbite
September 8th, 2003, 08:52 PM
Thanks this is extreamly helpful. Thanks for taking the time to write it.

Szafran
September 8th, 2003, 08:57 PM
after i put it in a text file what would i name the extention?

weaver_x7
September 8th, 2003, 09:48 PM
All java source files must have the .java extension in order to be compiled.

PM8228
September 8th, 2003, 10:05 PM
I apologize but I code, but am a newbie to Java. I read a lot of tutorials and such, but I still do not understand it. On the otherhand I can understand it perfectly if somebody taught me the bare basics I can pick up everything else real quick. Is that a problem for other people, and if so how did you rectify it? Thanks for the tutorials, even if they did not stick with me. The effort is and will be appreciated by many.

warriorfan808
April 11th, 2005, 12:20 AM
It's cool to see some newbies writing some tutorials on here. I'm pretty much new at programming as well, just started making GUIs in Java. Have you tried using JDK 1.5 yet? I've been seeing fewer people using System.out.println(); or System.out.print(); Lately, I've been seeing them use System.out.printf(); I normally stick with the old method, but this new method make concatination (spelling) simplier.

Also, have you all tried using JGrasp yet? I started using it and it seems pretty cool, but I've ran into a few instances when I was completely fooled into remembering to save my work.

For those of you who haven't used it, you can compile and run your program without saving. I've also ran into some problems where the changes weren't completely made and JGrasp didn't register them. I sucked big time. My program worked, but I guess JGrasp didn't see enough in the original program to make the change to the class file.