Java help

So i have decided to learn java becuase i cant get a graphics library in c working. just to make sure im getting the write idea so far:
1
2
3
4
5
6
7
public class nameOfTheJavaFile
{
     public static void main(String[] args)
     {
          data goes here
     }
}


and it seems like everything so far is an object so far.

Am i correct so far?
Yes, that is one of the main design decisions of Java. I don't really know Java, but that code doesn't look incorrect to me.
and also what is a .jar file and what is a .class file?
I think you'd have more luck going to something like Dreamincode.com, not this site, which is for C/C++.
i no its for c++ but i dont feel like making another account on a site that ill use once
1
2
3
4
5
6
7
8
public class nameOfTheJavaFile
{
     public static void main(String[] args)
     {
          data goes here //err ┬┐data? 
          //logic goes here
     }
}

> everything so far is an object so far.
Nope. There are basic types.
Also blocks aren't objects either.
Can't remember if the classes are objects too.
i wasnt saying that every line of code was an object. data types are classes. classes are what objects come from. i was saying that the basis of java is objects.
There are 9 primitive types in Java:
boolean (bool)
byte (signed char)
char (unsigned short)
short (signed short)
int (signed int/signed long)
long (signed __int64)
float (float)
double (double)
reference
The rest are classes. References are, in my opinion, a primitive, because references can only reference objects in Java and not primitives (which is why java has no support for reference-to-reference; references are themselves primitive). Think of Java references as C++ pointers that are always either valid or null, and you use . instead of -> to access fields and methods.

Java and C/C++ have strikingly similar syntax, mainly because Java was based on C. If you know C/C++, you shouldn't have a hard time adjusting to Java's syntax rules (they're nearly identical). It's the design patterns in Java that you will have a hard time adjusting to coming from C/C++.

Sometime soon you should see a post from rapidcoder explaining some other stuff and possibly disagreeing with/correcting me. He's the real Java pro around here, I just like Java as much as C++.
Last edited on
In Java, start class names with a capital letter: NameOfTheJavaFile, not nameOfTheJavaFile. This is the convention used everywhere, so if you do otherwise, your code would look strange.
and also what is a .jar file and what is a .class file


jar files are just archive files. I believe they're derived straight from .zip. Someone correct me if im wrong. Class files are the compiled source files (.java files).
Sample Hello World Program:
1
2
3
4
5
public class Main{ // class name is usually the name of the file
  public static void main(String args[]){ //used to specify the executer class
     System.out.println("Hello World"); //prints Hello World
  }
}


Compilation:
 
javac Main.java


Execution:
 
java Main //Here Main is the class file obtained after compiling 


IDE recommended : Eclipse or Netbeans

JAR( Java Archive) file is the final file obtained after the compilation. It can be compared with .exe file obtained after compiling a c++ program. It contains the class files and resources too(sometimes) . It also contains a manifest file used to specify the name of the main class which is responsible for running the project.

JAR files can be easily formed using the jar command with cvf flags on command prompt if you have installed the jdk and set the environment variables properly.

And why are you not able to get a graphics lib in C or C++ ?
I want to learn graphics in C++ but I haven't started yet . Should I concentrate on Java only because I already know Java?
Last edited on
@ResidentBiscuit: yep, you can actually rename a .jar to .zip and open it with windows explorer or whatever you cool guys use.
Yeah, you MUST have public class <filename> otherwise you get this error:
javac hello.java
hello.java:1: error: class Main is public, should be declared in a file named Main.java
public class Main{
       ^
1 error
@BHXSpecter

Yeah , But the filename can be different if the class is not public .
For example:
1
2
3
class Person{
 private String name ;
}


Save it in a file named human.java
javac human.java
It will compile successfully .

However , this class cannot run as it does not have the main function
java Person //error

You can use this class in any other file as well
If the class file is in a different directory use an import
statement
1
2
3
4
5
6
public class Game {
 public static void main(String args[]){
   Person myPlayer = new Person();

 }
}
Last edited on
Topic archived. No new replies allowed.