[Java] A Console Application.. (#1)
Next: [Java] An IO Class (#2)
Java isn't recommended as a first programming language. So if you've never programmed before, Java probably isn't for you.
I'd suggest starting with a scripting language, or one with an easier flow and syntax- like Python.
For beginner programmers, complicated syntax and a bunch of vocabulary isn't a good thing- it means months and months of bookwork about shit you don't even know yet.
So, I'm not going to explain all that "shit you don't even know yet", since Java, is not good for beginner programmers, IMHO.
So here's your console application. I'm assuming you have the JDK, and some "JDE", (Java IDE)- like Eclipse or Netbeans. As far as I know, they work on all stable operating systems- even Minix. (Yeah, I went there). And that's one of the great things about Java- it just works, and doesn't take too kindly to system-dependent crap. That's what (helps) keep it working on almost all systems.
Create a file called "hello_world.java" with the following code inside it:
PHP Code:
// use the scanner library
import java.util.Scanner;
// Create a class with a descriptive name
class hello_world
{
// Create a method named "main" of type" void"
// with that special argument that can take console
// commands
public static void main( String args[] )
{
// Make a string prompting the user to type something
String greeting = "Say Something:";
// Print the string "greeting" above
System.out.println( greeting );
// Take some input
Scanner in = new Scanner( System.in );
// Add the first line of input to a string
// named "something"
String something = in.nextLine();
// Print the string "something"
System.out.println( "You Said: " + something );
}
}
Then open up a terminal or a command prompt. 'cd' means Change Directory. Use cd to navigate to your file in your filesystem. If you saved it in a folder called "java" on your Desktop, for example, you can type (assuming your Desktop directory is inside "/home/john_doe/Desktop"). If you need to do some browsing, the "dir" command will show you all the files/folders in whatever directory you're in. So if you're in "C:\", typing "dir" would show you "windows", "Documents and Settings", or whatever you have in "C:\".
Code:
Example:
cd /home/john_doe/Desktop/java
dir
hello_world.java
Once you're in the directory with "hello_world.java", simply type (on the console):
Code:
javac hello_world.java
That will compile your java program or present you with an error message if something prevents it from compiling properly.
To run your java program, simply type:
And the program will run.
If you haven't noticed, Java requires instant knowledge of Object Oriented Programming. So you need to know that before learning Java.
The syntax is fairly easy for any OOP-competent programmer/scripter, so, have at it.
Static methods/variables in Java are basically, easier to use, because you don't have to include the class name in order to call the method/variable.
Notice this example:
PHP Code:
// use the scanner library
import java.util.Scanner;
// Create a class with a descriptive name
class hello_world
{
// Reserve this string for input
public static String input;
// Create a method named "main" of type" void"
// with that special argument that can take console
// commands
public static void main( String args[] )
{
// Make a string prompting the user to type something
String greeting = "Say Something:";
// Print the string "greeting" above
output( greeting );
// Take some input
input();
// Print the string "input" which was defined in the..
// (for lack of better word..) "main" scope of the class..
// Not to be confused with this "main" method in this class.
output( "You Said: " + input );
}
public static void input()
{
// Create Scanner for Input
Scanner in = new Scanner( System.in );
// Fill input String with a line of data from Input Scanner
input = in.nextLine();
}
public static void output( String out )
{
// Print a line of output to console
System.out.println( out );
}
}
Trying the above without the "static" keyword for the String input, and the methods "input()" / "output()" will need some tinkering.
Exercise 1.1:
As an exercise, figure out what tinkering needs to be done for the three static elements I noted in this paragraph.
Answer:
( Did you research (search google) first, before clicking this button? )
And there you have it, a very simple console application written in Java that you can all play with until I come back and make more. :thumbup:
(Feel like this, :ehh:, still?
Told you it wasn't for beginner programmers.)
Next: [Java] An IO Class (#2)
Re: [Java] A Console Application.. (#1)
"Java isn't recommended as a first programming language. So if you've never programmed before, Java probably isn't for you."
Then what is? Java is my first language and I'm starting to learn it very well. (Thank you Minecraft)
Re: [Java] A Console Application.. (#1)
Quote:
Originally Posted by
Gravious
Isn't recommended. There are tons of tutorials for Minecraft-based Java projects so that's why you may get it fairly well.
I believe I started with Visual Basic because it was simple English in which you programmed. After PHP and Python I learned C# which looks similar to Java on some subjects so in the end Java was cake.
I started with Java because I wanted to create my own games. Learning that Minecraft was created with Java I chose it. But now I'm learning C++, HTML, and CSS. But being that C# is closer to Java I think it might be good for me.
Re: [Java] A Console Application.. (#1)
Quote:
Originally Posted by
Scoin0
I started with Java because I wanted to create my own games.
infact, java is a bad choice for games, its very slow. however, notch proved its possible, still, saying to learn java for games is ridiculous.
Re: [Java] A Console Application.. (#1)
Quote:
Originally Posted by
VibeTribe
infact, java is a bad choice for games, its very slow. however, notch proved its possible, still, saying to learn java for games is ridiculous.
Look at RuneScape, it's running on Java.
Re: [Java] A Console Application.. (#1)
Quote:
Originally Posted by
WizCoder
Look at RuneScape, it's running on Java.
RuneScape and Minecraft aren't the best graphical games though. The good thing about those games are that they don't really need those graphics.
Re: [Java] A Console Application.. (#1)
Quote:
Originally Posted by
VibeTribe
infact, java is a bad choice for games, its very slow. however, notch proved its possible, still, saying to learn java for games is ridiculous.
I'm not gonna base all my games in Java. I just want to learn it. I have however been told that Java sucks and why am I choosing it to start creating games. T.T
Quote:
Originally Posted by
Gravious
RuneScape and Minecraft aren't the best graphical games though. The good thing about those games are that they don't really need those graphics.
I'm sure Java can do some nice graphical games...I'm created a game that doesn't really need graphics so..I'm ok :D I would love to learn C++ though
Re: [Java] A Console Application.. (#1)
Quote:
Originally Posted by
WizCoder
Look at RuneScape, it's running on Java.
Both RuneScape and Minecraft could be written more quickly JavaScript with better graphics and still run better. And they'd have support across almost every desktop and most phones/tablets.
What an interesting world we live in, where Javascript takes the ubiquity crown, even though Java was born for it.
Re: [Java] A Console Application.. (#1)
Java does kinda suck.. I code everything I need in EcmaScript5 these days....
Re: [Java] A Console Application.. (#1)
If you want to create games, I recommend learning RGSS.
Re: [Java] A Console Application.. (#1)
I think it's funny how people compare coding to graphical images. The graphic part has nothing to do with the codes, it has all to do with the artistic method one puts into it. This is like comparing Images to Videos, either one will look like shit if there's isn't any real artistic talent put behind it.
I'm actually interested in learning java for multiple reasons, and one of them being cross platform.