Thread: Java Basics

Results 1 to 4 of 4
  1. #1 Java Basics 
    Special Snowflake

    DeathKid's Avatar
    Join Date
    May 2013
    Posts
    388
    Thanks given
    191
    Thanks received
    96
    Rep Power
    383
    Hello & welcome to my quick guide. I thought I'd make a contribution to the community so I've decided to make this guide to help out new programmers.


    Introduction to Java
    A typical Java program has a user-defined class, these classes contain objects that interact with those from Java class libraries. Related classes are grouped into packages, many of which are provided with the compiler. You can also add your own classes into a package, this facilitates their use in other programs. The package
    Code:
     java.lang
    (which contains most of the commonly used classes is automatically imported to all programs). If you wish to add more packages you'll need to use the import statement.

    Example of importing packages:

    Code:
    import packagename.ClassName; 
    import packagename.subpackagename.Classname;
    import packagename.*;
    The first one would import the specified class, the second one with import the specified class which is inside the sub-package & the third one would import all of the classes contained inside of the package name.

    Introduction to Java Part 2
    A Java program must contain at least one class that contains a main method. The files that comprise your program are called source-files (src). The compiler converts your source-files into machine-readable code called bytecode.
    Example of a basic Java program:

    Code:
    import package.*;
    import package2.*;
    
    public class Example {
    
          public static type method(parameter list)
          {
           //CODE FOR METHOD GOES HERE
          }
    
          public static void main(String[] args)
          {
           //YOUR CODE GOES HERE
          }
    }
    As you have noticed above, the program contains block statements ({ & }). These block statements are used for methods, classes, and loops and conditional statements (loops and conditional statements when they contain more than 1 line of code. The class must be opened or closed with blocks and all data must be contained within the blocks (except for import statements and package names). The words class, public, static, void and main are reserved words, also known as keywords, i'll be talking about this later on in the guide.


    Types & Identifiers
    An identifier is a name for a variable, parameter, constant, user-defined method or user-defined class. In Java identifiers are sequences of letters, digits and / or the underscore character. However, identifiers may not begin with a digit. These identifiers are case-sensitive which means that capital and lowercase letters are not the same thing.

    Example:

    Code:
    public class Example {
    The words public & class are Java keywords while Example is your class-defined identifier.

    Built-In Types
    Every identifier in Java has a type associated with it. The primitive (a.k.a built-in types) all resemble different things.
    Examples:
    Code:
    int - An integer (Example: 4,-250,3000)
    boolean - A boolean (Either true or false)
    double - A double precision floating-point number (Example: 3.720, -312.2, 1.52)
    Note: if you try to store a value whose magnitude is too big you'll get an overflow error.

    Initializing Data & Casting
    We use casting to make data sets compatible with their types (examples below). Initializing data is when we give data types their values for later usage.
    Code:
    Initializing:
    
    int x;
    double y,z;
    boolean a;
    int count = 0; //count initialized to 0
    double a = 4.5; // a initialized to 4.5
    
    Casting:
    
    int total, n;
    double average;
    average = (double) total / n; //total cast to double and ensures real division is used
    
    Alternatively:
    
    average = total / (double) n;
    Semicolons & Whitespaces
    Imagine semicolons as periods in English. We use them at the end of each of our sentences, this will help you understand their concept. Whitespace in Java is completely ignored, with this being said
    Code:
    public class Example {
    public static void main(String[] args) {
    System.out.println("Hi");
    }
    }
    is the exact same as
    Code:
    public class Example {public static void main(String[] args) {System.out.println("Hi");}}
    We use spaces to make our code easier to read / understand.


    Operators
    Code:
    Arithmetic Operators:
    +  addition (Ex: 3+x)
    -  subtraction (Ex: 3-x)
    * multiplication  (Ex: 3*x)
    / division (Ex: 3/x)
    % remainder (Ex: 3%x)
    
    Relational Operators (used to express booleans and evaluate to either true or false)
    == equal to (Ex: a == b)
    != not equal to
    > greater than
    < smaller than
    >= greater than or equal to
    <= smaller than or equal to
    
    Logical Operators:
    ! not (Ex: (!found)
    && and (Ex: (a ==b && b == c)
    || or (Ex: (a != b || b != c)
    
    Assignment Operators:
    = assigns something = to something
    += this means this plus this stored in this (Ex: 2+=4 means 2 = 2+4)
    -= same as above
    *= same as above
    /= same as above
    %- same as above
    
    Increment & Decrement Operators:
    ++ increments by 1 (Ex: count++, if count was 3 it becomes 4)
    -- decrements by 1 (Ex count--, if count was 3 it becomes 2)
    I/O & Escape Sequences
    User input can be read in many different ways, one is using the Scanner class (import java.util.Scanner) and another is using GUI. For the sake of this basic guide i'll be explaining the Scanner one.
    Code:
    import java.util.Scanner;
    
    public class ScannerExample {
     public static void main(String[] args) {
      Scanner objectname = new Scanner (System.in);
      String example;
      System.out.print("Enter a string: ");
      example = objectname.nextLine();
     }
    }
    this will read a user-input and assign his input to the String example. Note: you can also use nextInt, nextDouble, nextFloat, etc. I'll leave this for you to explore on your own.

    Code:
    System.out.println(); //this will print a new line
    System.out.print(); //this will print on the same line
    We use quotation marks (" ") to represent string objects.
    Examples:
    Code:
    System.out.println("Hi"); //prints out Hi
    System.out.println(x); //prints out the value of x
    
    int[] array = {1,2,3,4,5};
    System.out.println(array); //prints out 1,2,3,4,5 on a new line

    Loops & Conditional Statements
    Code:
    if (condition)
    statement; 
    
    this means if the condition is true the statement is executed, else ignored.
    
    while (condition)
    statement;
    
    this means while the condition is true the statement is repeatedly executed, else ignored. Example: while (n>5) System.out.println(n);
    
    for (index value; until; increments or decrements)
    statement;
    
    this mean repeatedly executes until reached the set amount. Example: for (int i = 0; i <= array.size(); i++) System.out.println(array[i]);
    
    for-each loops will be explained in the next section.
    I'll continue the guide when I have the time, what will be included:
    *Recursive Programming
    *Dynamic Binding & Polymorphism
    *Constructors, Classes, Interfaces, Inheritance, etc.
    *Waterfall Program Design
    *Parameters, ArrayLists, Arrays, Two-Dimensional Arrays, etc.
    *Method Overloading & Method Overriding
    and more things .
    "Always remember that you are unique. Just like everyone else" - My Mom
    Reply With Quote  
     

  2. Thankful user:


  3. #2  
    Special Snowflake

    DeathKid's Avatar
    Join Date
    May 2013
    Posts
    388
    Thanks given
    191
    Thanks received
    96
    Rep Power
    383
    Reserved.
    "Always remember that you are unique. Just like everyone else" - My Mom
    Reply With Quote  
     

  4. #3  
    Special Snowflake

    DeathKid's Avatar
    Join Date
    May 2013
    Posts
    388
    Thanks given
    191
    Thanks received
    96
    Rep Power
    383
    Reserved.....
    "Always remember that you are unique. Just like everyone else" - My Mom
    Reply With Quote  
     

  5. #4  
    Registered Member
    Join Date
    Feb 2018
    Posts
    25
    Thanks given
    6
    Thanks received
    0
    Rep Power
    11
    one of the best things i have seen for explaining it like i know some stuff and i tell you now when i read the examples and everything else it makes sense! keep up the good work DeathKid!
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. [JAVA] Java basics
    By Sillhouette in forum Tutorials
    Replies: 5
    Last Post: 04-02-2012, 03:24 AM
  2. [JAVA] Java basics
    By Sillhouette in forum Tutorials
    Replies: 3
    Last Post: 03-31-2012, 10:52 PM
  3. [Java basics #1] Conversion.
    By blackaddiction in forum Application Development
    Replies: 19
    Last Post: 05-13-2010, 07:17 PM
  4. [Java] Basic socket accepting.
    By Alpinestars in forum Application Development
    Replies: 4
    Last Post: 07-27-2009, 09:18 AM
  5. [Huge-Tut]MagicHandler.java [Basically full magic]..
    By Santa Clause in forum Tutorials
    Replies: 55
    Last Post: 05-16-2007, 11:55 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •