Hi, its been a long time since i last posted on these forums but i am back and I thought i would do something usefull so I want to help people who are learning a programming language and they want to know java or just for some help with it.
If you would like help with anything please post it at the bottom or if your just curios on what it
When I put code: it means the next section will be code at the end look for a ! and that means the end of the code.
so lets get started, yes thats right if you've learnt a language you know the first step is doing a hello world application so lets get started we will explore what it actually means after we have the code down.
code:
public class helloworld{
public static void main (String[] args){
System.out.println("hello world");
}
}
!
Now time for the stuff you need to know, what does this code do. Well public class means well a class that can be used by other classes if it was private class it could only be used by methods inside itself but it could still tell other classes what to do. public static void main(String[] args) is the method called whenever you start a java program you need it or your program wont run and you will just get errors. Now if you know about importing you might be going "but how will that work you need to import java.lang" well no lang is already implemented in your class you dont need to worry about it. What System.out.println(""); is, is an operation its saying look in in System for the out method then look in there for println and ("") is a paramater but we will go into those later. {} mean open and close so each section is cut up like that you will get used to them as we go on.
Ok lets get onto some usefull stuff like objects variables and methods.
objects are used all the time in java to referance to things in other classes or even your own and also are used to seperate out diffrent values from the same variable.
Right same as last time code then explanation.
code:
public class objects{
public static void main(String[] args)
{
methodclass mc = new methodclass();
mc.rightthing();
}
}
public class methodclass{
public void rightthing(){
System.out.println("random thing draw :D");
}
}
!
WOW we went through alot in that, ok your probably confused with whats going on here. making an object of something means somthing unique to refer to this means that if i got it to return a random value via two diffrent objects from the same class and from the same variable i would get diffrent number. these are extremely usefull and we will be using them all the time in these tutorials.
voids are very important infact without them you wouldn't be able to write in java at all they are a fundimental function. to call a void we use our object and call the function through that. so lets say i made a class called funyclass it had a void that said lol and the void was called randomness so in my main method i would make this object funyclass fc = new funyclass(); fc.randomness(); and we would get the output lol i will put the actual code for this below if yor abit lost on what I just said. (ps sorry for my bad spelling D:)
code:
public class mainclass{
public static void main(String[] args){
funyclass fc = new funyclass();
fc.randomness();
}
}
public class funyclass{
public void randomness(){
System.out.println("lol");
}
}
!
Time for ints and Strings these are another fundimental thing you need to know to be a java programmer and there very simple to use.
EXAMPLE TIME!!!!
code:
public class exampleint{
private int x = 0;
public static void main (String[] args){
exampleint e = new exampleint();
e.addndraw();
}
public void addndraw(){
x++
System.out.println(x);
}
}
!
This code is very simple it calls a function but 1 thing we havent gone over which is boolean operators ++ -- / == * ^ % !
what ++ means is add 1 to that int you could also do these x = x + 1 or x += 1 same for subtraction for division you would need a seperate variable to store it like so.
code:
public void divide(){
int x = 123;
int store = x/32;
System.out.println(store);
}
it is the same for multiplication and most other things.
Lets put these new things to use time for a calculator that adds random numbers. dont worry about the (int) (Math.random() * 100); atm.
code:
public class randomadd(){
private int fnum = (int) (Math.random() * 100);
private int snum = (int) (Math.random() * 100);
private int finalnum = 0;
public static void main(String[] args){
randomadd add = new randomadd();
add.calculateanswer(fnum,snum);
}
public void calculateanswer(int fnum1, int snum2){
finalnum = fnum1 + snum2;
System.out.println(finalnum);
}
}
!
Right the paramaters (int fnum1, int snum2) mean when you call that method yo uneed to give those values to it they can be strings variable even images. we will go into them more next tutorial but they allow for lots more things to be done at the same tiem and also allow numbers to travel through classes "internatioinally" if you will.
Thats it for the first post will be updated very soon hope you enjoy learning java !!!