Tuesday, October 8, 2013

Is the final variable really constant in Java?

My answer is no. Oh, it was a bold answer. Let's see why.

Constant is something that can not be changed. The final variable by definition is a variable that can be initialized once.

The difference is slight. If the variable can be initialized only once, how can we change it?

Let's think about objects. We can initialize a variable to the object. In fact, we initialize the variable to the address of the object in the main memory).

Recall how the objects are stored in Java. See the picture below.

In the variable name1 we store the address of the place in the main memory where the object is stored, which is 42 in the picture).

When we declare a variable as final and initialize it, we can not change the address after that, but we can change the object!

Let's look at an example.

Tuesday, October 1, 2013

Why does Java use the Unicode?

I like this description, so I quote it. It answers the question why java uses Unicode.

The Java programming language uses the Unicode character set for managing text.

A character set is simply an ordered list of characters, each corresponding to a particular numeric value.

Unicode is an international character set that contains letters, symbols, and ideograms for languages all over the world. Each character is represented as a 16-bit unsigned numeric value. Unicode, therefore, can support over 65,000 unique characters.

Only about half of those values have characters assigned to them at this point. The Unicode character set continues to be refined as characters from various languages are included.

Many programming languages still use the ASCII character set. ASCII stands for the American Standard Code for Information Interchange.

The 8-bit extended ASCII set is quite small, so the developers of Java opted to use Unicode in order to support international users.

However, ASCII is essentially a subset of Unicode, including corresponding numeric values, so programmers used to ASCII should have no problems with Unicode.

John Lewis. Java Software Solutions: Foundations of Program Design.

Sunday, September 29, 2013

What data must I check in PHP and how?

I hope you know the golden rule that every and each PHP programmer must know.

The First Rule

Check all data received from user!

All the data that we've got in the global arrays such as $_POST and $_GET must be checked.

Use filter_input() method to delete not needed symbols.

There are some types of filters for this method. Some useful examples:

$name = filter_input(INPUT_GET, 'name', FILTER_SANITIZE_STRING);
$email = filter_input(INPUT_GET, 'email', FILTER_SANITIZE_EMAIL);

There is one more rule.

The Second Rule

Friday, September 27, 2013

How to compare strings in Java

String in Java is not a primitive type, such as integer, boolean etc.
Every string is an object.
We can compare strings using method equals() or the identity operator ==.
Let's separate between the two types of comparing here.
  • While using equals() the comparison is done by characters. String name1 = "Hello, world!";
    String name2 = "Hello, world!";
    if (name1.equals(name2))
        System.out.println("The names are the same.");
    // Output "The names are the same."

    That means string "Hello, world!" is equal to the second "Hello, world!", because these strings have the same characters.
  • While using == the pointers to strings are compared. if (name1 == name2)
        System.out.println("The names are the same.");
    else
        System.out.println("Oh, no!"); // Output "Oh, no!"
    When we write this code:
    String name1 = "Hello, world!"; we have this picture in the main memory:

    The variable name1 holds the address of object in the memory (in our example the address is 42).
    name1 is a pointer to the location in memory where the object is held.

    Let's create strings as different objects.

How to compare float numbers safely?

This short article explains why we cannot use "==" to compare floats and how to compare floats right.
You can try to run this code in Java to understand the problem. double a = 1.0f / 3.0f;
double b = a + a + a;

System.out.println(a); // Output 0.3333333432674408
System.out.println(b); // Output 1.0000000298023224
System.out.println(1 == b); // Output false

Why?

If the compared values are the results of computation, they can be not equal.
Binary representation of the number 1/3 cannot be precise, because the place where the number is stored is limited. So the number rounding was made: 1/3 = 0.333.. = 0.(3) = 0.3333333432674408
So we have b = 1.0000000298023224 that is not equal to 1.0;

How to get round that problem?

Thursday, September 26, 2013

How to replace string in the project using terminal in Ubuntu?

We want to replace the string "regexp" with the string "replacement" in the terminal.
Let's say our project path is /home/www/project_name.
Actually regexp string is a regular expression pattern for our string.

This command works right:
cd /home/www/project_name
find . -type f -print0 | xargs -0 sed -i 's/regexp/replacement/g'
Let's see what the command means.
I will translate it to the normal language for human beings.

Go to my project folder:
cd /home/www/project_name Find all files (-type f) in my project folder:
find . -type f Use \0 delimiter (and not whitespace) to separate between the found pathnames in the output.
(We don't want whitespaces, because filenames can contain whitespaces and we will not be able to separate between pathnames).
-print0 This is a pipe. It means all the output that we have got before this sign "|" becomes an input for a next command (xargs in our case).
| xargs takes the output (pathnames that the command find found).
We use -0 because pathnames are terminated by \0 (instead of by whitespace). We know because we specified (find -print0).
xargs -0 sed - stream editor for filtering and transforming text.
-i allows to edit files in place.
-g replace all matches, not just the first match.
Replace our "regexp" with "replacement". sed -i 's/regexp/replacement/g'

What do lazy and greedy regular expressions mean?

Greedy expression tries to get as much as possible.
If you write a regular expression like this
<.+>
for the string:
<p>My expression</p>
you will get all the string
<p>My expression</p>
Oh, yes.. this thing is really greedy.

Let's take a look at the lazy one. If you write a regular expression like
<.+?>
We have just added "?". That means we want the smallest possible group. Now we have this result:
<p>
Here you are. The laziest thing you have ever seen :)

Tuesday, September 24, 2013

What screen resolution should I use for my web page design?

Not so long ago the answer was - 1024 x 768.
But you know, "the times they are changing".
Browser Display Statistics shows:
Today, most visitors have a screen resolution higher than 1024x768 pixels.
Of course you should know your audience and make the decision about resolution accordingly.

Thursday, April 4, 2013

JavaScript: How to get all properties of object

While using JavaScript statement for… in, we get functions of objects. If you need only properties without functions, use that:
for (key in object) {  
   if (object.hasOwnProperty(key)) {  
      ...  
   }  
}

Thursday, March 14, 2013

Symfony2: How to find out the form errors

How to find out the form errors in Symfony2. Just add that code to your Controller:
if ($form->isValid()) {
...

} else {
    foreach($form->getChildren() as $child) {
        foreach( $child->getErrors() as $error) {
            print($error->getMessageTemplate()); 
        }
    }
}
It takes into consideration only the errors of the form fields. If you want to check errors of the form too, add that:
foreach ($form->getErrors() as $error) {
    print($error->getMessageTemplate());
}

Wednesday, March 13, 2013

I am a cool php programmer :)


Tuesday, February 26, 2013

Some useful BASH shell hot keys and useful tips.

Check out these hot keys. They can help a lot to save your time.
Since I started using them, I'm just a new man :)

This one is so important, that I'll write it separately.
Ctrl+R To search in previous commands. It displays the last occurrence.
Ctrl+R+R It displays the occurrence before the last.

These commands are also very useful. When I say they are "useful", I really mean that I use them! :)
Ctrl+K To delete the characters from the cursor to the end of the line.
ESC+D To delete the characters from the cursor position to the end of the word
Ctrl+A To move the cursor to the beginning of the command line
Ctrl+E To move the cursor to the end of the command
Ctrl+L To clear the screen, puts the current command line at the top of the screen

And I've got one additional tip for you. Use aliases!
Don't be lazy. It's so worth it!
To create your own alias, edit the file ~/.bashrc. Put there your alias.
Here is my alias that I use very frequently:
 alias pr='cd /var/www/myprojects' 
In this file you can see already added aliases. To check them out, you can also use alias command.

Wednesday, February 20, 2013

How to get list of available timezones in php

<?php
    print_r( DateTimeZone::listIdentifiers( ) ); 
?>

Saturday, February 16, 2013

Some basic UNIX commands

Today I will write down some basic UNIX commands.
"cd -" will switch you to the previous directory.
testuser@test:~/Downloads$ cd ../Desktop/
testuser@test:~/Desktop$ cd -
/home/testuser/Downloads
testuser@test:~/Downloads$ 

"cd ~" or "cd" will switch you to your home directory.
testuser@test:~/Downloads$ cd ../Desktop/
testuser@test:~/Desktop$ cd -
/home/testuser/Downloads
testuser@test:~/Downloads$ 

"pwd" (print working directory) is used to output the path of the current working directory.
testuser@test:~/Desktop$ pwd
/home/testuser/Desktop