Tag: Variables

Java Initialization Blocks

Java has some features that are not well know from a lot of Java Developers, even developers with 5 and 10 years of Java Experience.As a Oracle/Java trainer I must study all of them to teach my students and prepare them well for the Java Certification.

One of these bot well know features are java Initialization blocks. One of these are blocks runs before the JVM runs any code (at class loader level) and the other one runs at constructor time.

Lets see an example and an explanation:

Instance Block
class MyClass {
private int x;
{
x = 7;
System.out.println(“This is called inside constructor, just after super(…)”);
}

public MyClass() {
System.out.println(“the constructor”);
}
}
Static Block
class MyClass {
private int x;
public static int y;
static {
y = 7; // y must be static!
System.out.println(“This is called by class loader before your program runs!”);
// x = 5; it’s illegal because this is class context and not instance context
}

public MyClass() {
System.out.println(“the constructor”);
}
}

So, there are 2 types of code initialization blocks:

Instance

When we talk about instances we talk about objects, something that was already created and has memory allocated.

The instance code block is executed right after the call to super in any constructor called.

It’s a way to initialize variables in all constructors without write code in any of them, which it’s a great way to organize initialization code, too.

Static

Static context it’s called class context too. Everything that is static it’s only visible when we use de class name before the field, method or inner class. (ok, unless we use import static…)

When we talk about classes we are talking about the blueprint or definition of the object. This definition “runs” before the instance is created, so the static initialization block runs before the java interpreter and before any constructor called. So, who runs the static initialization blocks? The class loader of the Java Virtual Machine.

This is the place to initialize ONLY static variables, because they are the only ones that are visible at this time and in this context.

In the future I will post more about not so know java features.

Technorati Tags: ,,,,,,,,,
Windows Live Tags: Java,Initialization,Blocks,Instance,Static,constructor,initialize,variables,context,class
WordPress Tags: Java,Initialization,Blocks,Instance,Static,constructor,initialize,variables,context,class

Learning PHP – Step 3 Basics

Following previous posts, lets start with the basics.

Variables:

– Always start with $ and can have the following chars _ [a-z] [A-Z] and [0-9]. the first symbol must be an _ ou a letter.

$count = 0;
$name = “José Cruz”

What about types? If you know python, ruby or the “var x = 0;” construction from C# you know how PHP works. It converts the variable for the type defined by the value initialized. For instance: $count = 0; will be an “integer”.

Lets use some variables in our script:





Learning PHP – Step 2


$name = "José Cruz";
echo $name . " is starting to understand PHP!";
?>

If you run the script above it will display “José Cruz is starting to understand PHP!”

PS: The dot between $name and “ is starting… ” is the concatenation in strings. This is new. Other languages usually use the plus sign or some function to do it.

The operators are the same as C or mainstream languages:

Arithmetic: +, ++, –, –, *, /, %

Assignment: =, +=, –=, *=, /=, %=, .=

Comparasion: ==, <=, <, >= >, <>, !=

Logic: &&, ||, !

PS: You can use “(“ and “)”

Some examples:

$a = 5 + 2 * (7 + 5);
$a++;
$b = $a –4;
$c = $a / $b;
$even = 10 % 2;
$a+=7; // same as $a = $a + 7;
$even = 10;
$even %= 2;
$verytrue = 2 < 7;
$veryfalse = 6 >= 3;
if( $a > 3 ) …. // future post we talk about if and other statements.
if( $a > 3 && $b < 10 || $even == 0 ) …

As with other languages:

– Unary operators precede all other ones ( “!” )

– then: *, / and &&

– Last: +, –, ||

Lets try this in our php website:





Learning PHP – Step 2


$name = "José Cruz";
$even = 10 % 2;
$odd = 10 % 3;
$someValue = $even * 4;

echo $name . " is starting to understand PHP with some operators" . "
";
echo "even values: ". $even . "
";
echo "odd values: ". $odd . "
";
echo "some value = " + $someValue . "
";
?>

Run it!

Next post: statements.

Technorati Tags: ,,,,
WordPress Tags: language,programming,Variables,Basics,PHP

Bad Behavior has blocked 82 access attempts in the last 7 days.

Hyper Smash