Learning Programming #2.0 Learning Java: Syntax

The java syntax may seem hard for beginners, but it can be learned using some simple rules.
In my last post you learned about lists which are sometimes useful in programming.

There are a few that allow you to follow the java syntax easily, if you apply them all:

  1. You have to import most of the things you use:
    Except from Math and String every other object you use needs to be imported like this:
    import path.ObjectName;
    The path is just the relative path inside your project if you import an Object of your own or the path inside the library you are importing from. The individual folders of the path are separated by "." instead of "/" or "\".
    You can look up the path of any library in the internet.
    For example if you want to import List out of the java standard library the import statement will look like this:
    import java.util.List;
  2. Except from imports everything else in java has to be inside of a class.
    You can define a class with
    class name {…}
    Everything inside the brackets will be part of the class. Like with variables you can choose any name you want except if the name is also a java-keyword.
  3. After every statement you have to put a ;.
    A statement can be variable declaration(creating a new variable):
    type variable;
    type variable = someValue;
    Object variable = new Object(…);
    It can a variable assignment:
    variable = someValue;
    It can be a function call:
    doSomething(…);
    variable = getSomeValue(…);
    But it can also be return/break/continue:
    return;
    return someValue;
    break;
    continue;
  4. For every opening bracket(( { [) there has to be a corresponding closing bracket() } ]) somewhere in your code and you cannot have structures like (…{…)…}.
  5. You need to use {…} every time you are using if, else, while, foror when creating a function:
    if(someThingIsTrue) {…}
    else {…}
    while(someThingIsTrue) {…}
    for(int i = 0; i < length; i++) {…}
    returnType functionName(…) {…}
    There are some exceptions to this, but when starting you won't come across them too often.
  6. Every statement except for variabe declaration has to be inside a function or a similar construct.
    Anoter way of expressing this rule would be:
    Every statement except variable declaration must have at leats two { in front of it.
  7. Your program should have
    public static void main(String [] args) {…}
    somewhere. This is the java main method, which is executed once at the beginning of your program. If your program should repeat several times(like a game or simulation) you would need to put a while(true) {…} somewhere in this. Anything your program has to be in some way called from this main function.
    You may rename args in any way, but the rest has to be in the form presented above!
  8. Functions sometimes have arguments. You have to match their number, their types, and their order!
  9. Make sure you assign all your variables before you use them.
    Assignment can be done during declaration or seperately:
    type variableName = someValue;
    or:
type variableName;
…
variableName = someValue;

Here you can see a small program that is syntactically correct(by the way it can draw fractals, but that doesn't matter here):
Screenshot from 2019-05-13 15-24-57.png
As you can see in the image, there is a way to declare multiple variables of the same time in one line:
type name1 = someValue1, name2 = someValue2, …;
Or without initial assignment:
type name1, name2, …;
I wouldn't recommend using these for beginners, but they can be sometimes more readable, when used for variables of the same context like x and y.
I think structures like these are what confuses newcomers the most. They are not essential to programming, but they are sometimes used to simplify, but for people who don't know them they can be confusing.
That's why I will try not to include them here too often.

Task

Just write a program that is syntactically correct and have fun with it!

Sort:  

Absolutely, learning a new programming language like Java can be both exciting and challenging. If you're facing difficulties with your coding homework, considering external help is a wise decision. Websites like CWAssignments.com offer valuable assistance for students in need. They provide expert guidance to help you grasp Java's syntax and complete your assignments effectively. Remember, seeking help when you're stuck is a sign of dedication to your learning journey.

Coin Marketplace

STEEM 0.19
TRX 0.17
JST 0.030
BTC 79435.01
ETH 3189.92
USDT 1.00
SBD 2.70