Wednesday, February 12, 2014

jQuery / Check if checkbox is checked

How to check if input checkbox is checked?

$('#inputId').is('checked');
return true || false


jQuery
 

 

 

 

 
 
     
    Check checkbox
 
    

Java / Exception

There are three exceptions components:
- try
- catch
- finally

First example demonstrate what will happen when we don't use exception handling.

public class Main 
{
 public static void main(String[] args)
 { 
  int a = 10;
  int b = 0;
  float result;
  result = a / b;    
  
  System.out.println("Program end");
 }    
}

Output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
 at test.Main.main(Main.java:45)

Program fails.

Second example demonstrate program with exception handling.
public class Main 
{
 public static void main(String[] args)
 { 
  int a = 10;
  int b = 0;
  float result;
  
  try 
  {
   result = a / b;   
   System.out.println(result);
  }
  catch (ArithmeticException e) 
  {
   System.out.println("Exception catched:");   
      e.printStackTrace();
  }
  finally
  {
   System.out.println("Block finally");
  }
  
  System.out.println("Program end");
 }    
}

Output:

Exception catched:
java.lang.ArithmeticException: / by zero
 at test.Main.main(Main.java:48)
Block finally
Program end

Exception catched.
Program didn't fails and run to the end.


TIP: Sequence of block is important!
// That code will couse error - code will not compile
try {

} 
catch (Exception e) {
    
} 
catch (ArithmeticException e) {
    
}

Output:

Unresolved compilation problems: 
Unreachable catch block for ArithmeticException. It is already handled by the catch block for Exception


TIP: Block finally is optional. Never mind whether Exception will occur (will be catched) or not. Block finally will execute everytime.

Tuesday, February 11, 2014

Hibernate - Entity

@Entity annotation - declares class as entity
Class User has to have setters and getters.
We defined table name by @Table(name="user") annotation. If these annotation will be omitted the name of table will be generated using class name.

@Entity
@Table(name="user")
public class User 
{
 @Id
 @GeneratedValue
 @Column(name="id", unique=true, nullable=false)
 private int id;
 
 @Column(name="login")
 private String login;
 
 @Column(name="password")
 private String password;
 
 @Column(name="name")
 private String name;
 
 @Column(name="surname")
 private String surname;

 
 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getLogin() {
  return login;
 }

 public void setLogin(String login) {
  this.login = login;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getSurname() {
  return surname;
 }

 public void setSurname(String surname) {
  this.surname = surname;
 }
}

Java / Polymorphism

Polymorphism

To demonstrate how polymorphism works we need base class named Person.
class Person 
{
 public void showDescription()
 {
  System.out.println("I am Person");
 }
}

// Class Boy extends class Person and override "showDescription()" method.

class Boy extends Person
{
 public void showDescription()
 {
  System.out.println("I am Boy");
 }
}

// Class Girl extends class Person and override "showDescription()" method.

class Girl extends Person
{
 public void showDescription()
 {
  System.out.println("I am Girl");
 }
}

// Class PersonService is used to do some action.
// We print only kind of person.

class PersonService
{
 // Method agrument: object person
 public void showPersonDecription(Person person)
 {
  person.showDescription();
 }
}

// Start program

public class Main 
{
 
 public static void main(String[] args)
 { 
  // Create three kinds of persons
  // Each object (p01, p02, p03) is assigned to class "Person".
  Person p01 = new Person();
  Person p02 = new Boy();
  Person p03 = new Girl();
  
  /* print persons description */
  new PersonService().showPersonDecription(p01);
  new PersonService().showPersonDecription(p02);
  new PersonService().showPersonDecription(p03);
 }    
}


Explanation:

There are two classes which extends basic class Person.
Then method showDescription() is overridden.

Class PersonService
Here is the key: Class PersonService has got one method which prints decription of given object of class Person (object of class Person, not class Boy or class Girl).

Class Main
Create three objects of class Person and pass it to the method of class PersonService.

TIP: Do you know that object is passed by reference? :)

Result of the program is:
I am Person
I am Boy
I am Girl

Polymorphism allows to work on the higher level of abstraction.
Now you can add new functionality to class Person and use it in every class which extends class Person.
You can simply add new class which extends class Person (for example class Baby) and don't have to change any functionality of class PersonService!

Reference:
Java official tutorial

Java / String equals() vs "=="

Comparing Strings in Java.

public class Main 
{
 
 public static void main(String[] args)
 { 
  String a = new String("abcd");
  String b = new String("abcd");
    
  if(a == b)
   System.out.println("true");
  else
   System.out.println("false");
  
  if(a.equals(b))
   System.out.println("true");
  else
   System.out.println("false");
 }  
}

The output will be:
false
true

Explanation:
The operator "==" check if String "a" and String "b" refers to the same object - to the same place in the memory.
Method equals() compare the content of Strings (content of two objects).

Take a look at second example;

public class Main 
{
 
 public static void main(String[] args)
 { 
  String a = new String("abcd"); 
  String b = a;
    
  if(a == b)
   System.out.println("true");
  else
   System.out.println("false");   
 }  
}

Now the result is:
true
true

Explanation:
The operator "==" check if String "a" and String "b" refers to the same object. Yes, it refers to the same object (String b = a;).

Remember:
The operator "==" check if objects refers to the same memory location.
Method equals() check the value of the object.

Monday, February 10, 2014

Java / field, method: modifiers

Access to members of class.
Modifiers of field, method:

public
- is visible everywhere

private
- is visible only in owner class

protected
- is visible in owner class and subclass but only in the same package (and sub-package)

no-modifier (also known as package-private)
- is visible in owner class and subclass in the same package (not in sub-package)


EXAMPLES:

Modifier: public

package test;
class Car
{
 public String brand; // visible everywhere

 public String getBrand() {
  return brand;
 }
 public void setBrand(String brand) {
  this.brand = brand;
 } 
}

Possible use:

package test;
import test.Car;
public class Main 
{
 public static void main(String[] args)
 {
  Car car = new Car();
  tc.brand = "Opel";  // OK
  // OR
  tc.setBrand("Opel") // OK
 }  
}

Modifier: private

package test;
class Car
{
 private String brand; // visible only in own class

 public String getBrand() {
  return brand;
 }
 public void setBrand(String brand) {
  this.brand = brand;
 } 
}

Possible use:

package test;
import test.Car;
public class Main 
{
 public static void main(String[] args)
 {
  Car car = new Car();
  tc.brand = "Opel";  // field is not visible (Exception in thread "main" java.lang.Error: Unresolved compilation problems: )
  // But you can do that way
  tc.setBrand("Opel") // OK
 }  
}

Modifier: protected

package test;
class Car
{
 protected String brand; // visible only in package "test" and also in each sub-package of "test"

 public String getBrand() {
  return brand;
 }
 public void setBrand(String brand) {
  this.brand = brand;
 } 
}

Second class in sub-package

package test.subtest;
class SportCar extends Car
{
  
}

Possible use:

package test;
import test.Car;
import test.SportCar;
public class Main 
{
 public static void main(String[] args)
 {
  Car car = new Car();
  tc.brand = "Opel";  // OK    
    
  SportCar sportCar = new SportCar();
  sportCar.brand = "Ferrari";  // OK  
 }  
}

Modifier: no-modifier (package-private)

package test;
class Car
{
 String brand; // visible only in package "test" (not in sub-package of "test")

 public String getBrand() {
  return brand;
 }
 public void setBrand(String brand) {
  this.brand = brand;
 } 
}

Second class in sub-package

package test.subtest;
class SportCar extends Car
{
  
}

Possible use:

package test;
import test.Car;
import test.subtest.SportCar;
public class Main 
{
 public static void main(String[] args)
 {
  Car car = new Car();
  tc.brand = "Opel";  // OK    
    
  SportCar sportCar = new SportCar();
  sportCar.brand = "Ferrari";  // field is not visible 
 }  
}


Reference:
Java official tutorial