• Home
  • About
  • Java
  • Java Applications
  • Java Games
  • Java Mobile
  • Java Class
  • Java AI
  • Java Tutorial

Java

Exception handling in Java

by Calin on Monday, March 15th 2010     No Comment
in Java, Java Applications, Java Class, Java Mobile, Java Tutorial   Tags: Java, Java Application, Java Programming, Java Tutorial

The term exceptions refers in Java to preventing an error that has a good chance of occurrence. Exceptions may occur for various reasons and can have different levels of severity. For example, if you want to load a file that does not exist on disk, if a file doesn't contains correct data, if a network connection fails, if there in an attempt illegal access of a bad memory, in all these cases must treat these cases.

In such a moment when an error occurs during the execution, will be generated an object of type exception that contains:

  • Information about that exception;
  • State of the program when the exceptions occurs;

Creating an exception object type is called throwing an exception. When the method generates (throwing) one exception, the execution system is responsible for finding a sequence code from one method to treat. Search is done recursively, starting with method that generated the exception and going back on line calls to that method. Sequence from one method to treat a specific exception called exception handler, and the interception and treatment it is called catch the exception.
…read more about Exception handling in Java

Java Control Structures

by Calin on Tuesday, March 9th 2010     No Comment
in Java, Java Applications, Java Class, Java Games, Java Mobile, Java Tutorial   Tags: Java, Java Application, Java Programming, Java Tutorial

A Java program  execution is controlled by control structures or instructions that are executed for their effect and not for the fact that return values.

• Instructions decision: if-else, switch-case;
• Instructions jump: for, while, do-while;
• Instructions for handling exceptions: try-catch-finally, throw;
• Other instructions: break, continue, return;

In this article you can find examples to how you can use the decision and jump control structures.

If Else

This is an example how you can use If Else structure.

class MyFirstClass{
   
    public static void main(String args[]) {
        int i = 5;

        if (i <= 5)
            System.out.println("The number is lower than 5");
        else
            System.out.println("The number is greater than 5");

    }
   
}

Switch Case

This is an example how you can use Switch Case structure

class MyFirstClass{
   
    public static void main(String args[]) {
        int i = 2;
        switch (i) {
        default:
            System.out.println("This is the default case");
            break;
        case 1:
            System.out.println("This is the first case");
            break;
        case 2:
            System.out.println("This is the second case");
            break;

        }

    }
   
}
…read more about Java Control Structures

Java Variables

by Calin on Thursday, March 4th 2010     No Comment
in Java, Java AI, Java Applications, Java Class, Java Mobile, Java Tutorial   Tags: Java, Java Application, Java Programming, Java Tutorial

A variable is a memory location that has an associated type, also a variable contains a value that is compatible with the type variable. The value of a variable may change following an assignment or the use of operators + + and – -. Compatibility assignment of default values and all variables are checked during compilation, except the table, for which verification is done during the execution of Java programs. Variables may be of primitive or references type to objects. Whatever their type to be used variables must be declared and, possibly initialized.

javahdvasdas

  • Declare variables: Type VariableName;
  • Initialization of variables: Type VariableName= value;
  • Declare constants: Final Type VariableName;

Is possible to declare and initialize multiple variables or constant of the same type in one statement, and this you can do like here: Type variable1 [= value1], variable2 [= value2 ],…;

Depending on where the variables are declared, these are divided into the following categories:

  1. Variables States – these are declared within a class visible to the all the ways that class and other classes according to their level of access;
  2. Parameters methods, visible only in the method;
  3. Local variables declared in a method, visible only in the method that;
  4. Local variables declared in a block of code, visible only in the block;
  5. Parameters to treat exceptions;

 

An example how to use a variable that is initialized:

  1. public class FirstClass {
  2.  
  3. public static void main(String args[])
  4.   {
  5.     String FirstVariable="variable"; // declare the variable and initialize with a value
  6.     System.out.println("My first " + FirstVariable);
  7.   }
  8.  
  9. }

First class in Java

by Calin on Tuesday, March 2nd 2010     No Comment
in Java, Java Applications, Java Class, Java Tutorial   Tags: Java, Java Application, Java Programming, Java Tutorial

In this article we make an example of a class in Java, an example which includes the main method. To begin you must have Eclipse installed.

The first step is to open Eclipse and then select from the menu: File -> New -> Java Project or Project ;

jisfi01

After you  choosing a name for your project and select the folder where all files will be stored for the project, the next step is to click the Finish button.

To write the first class you must first create the class file and to do this follow the next steps: Go to your project and on the src folder Right Click and then select New -> Class. Here you must give your class a name and of course click Finish button. Now, your first class file was created.

jisfi02

As you can see, the classic statement FirstClass is the same as the filename class, in Java that is required as the class name to be identical to the filename class.

  1. public class FirstClass {
  2.  
  3. {

Now we will write the first method in Java, a method that will display on the screen the following text: "My first Java class".

  1. public class FirstClass {
  2.  
  3.  public static void main(String args[])
  4.   {
  5.       System.out.println("My first Java class");
  6.   }
  7.  
  8. {

This class has the effect to displays the message from double quotes in the console. To Run this class you should press the Run button or Ctrl + F11.
…read more about First class in Java

  • Twitter
  • Java

Advertisment

  • ads ads ads ads ads ads

Popular Tags

  • Java Java Application Java Programming Java Tutorial

Recent Commented

Most Commented

Categories

  • Java
  • Java Applications
  • Java Games
  • Java Mobile
  • Java Class
  • Java AI
  • Java Tutorial

Recent Articles

  • Exception handling in Java
  • Java Control Structures
  • Java Variables
  • First class in Java

Blogroll

    • Funny commercials
    • Stuff
©2010 Java
Free WordPress Theme by Mkels
Valid XHTML       Valid CSS       WordPress 2.8.6