abalfazl hossein

Ranch Hand
+ Follow
since Sep 06, 2007
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
4
Received in last 30 days
0
Total given
49
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by abalfazl hossein

this line has problem in this propgram: MyOuter2.MyInner inner = new MyOuter2().new MyInner();

9 years ago
Another code:


This code throws this exception. why?

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
symbol: class MyInner
location: class myouter2.MyOuter2
at myouter2.MyOuter2.main(MyOuter2.java:34)
Java Result: 1



9 years ago
Thanks for answer


This code doesn't compile and throw exception.Why?

9 years ago


This code works fine when I use makeInner method,But without, does not compile. Why?
9 years ago

It is NOT required that if two objects are unequal according to the
equals(java.lang.Object) method, then calling the hashCode() method
on each of the two objects must produce distinct integer results. However,
the programmer should be aware that producing distinct integer results for
unequal objects may improve the performance of hashtables.


JAVA SCJP book

so it will almost always return a different hashcode for distinct (ie, "!=", as opposed to "not 'equal'") objects.



It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results.



http://www.javaranch.com/journal/200407/ScjpTipLine-hashCodesUncovered.html


I think that it is better to say:

if two object are equal according equal method,Then calling hashcode must return same integer result.Right?
9 years ago
Is it the right reason in order to override:

Because when we customize equal method so it focus on special variables,We must change the hash code too in order to match with it, so hashcode also focus on those special variable.

Right?

For example if the class has 3 variable and we know equal two objects if x1 and y1 be equal to x2 and y2,and wedon't care about z.

So we must override hashcode so it creates the code according to x and y, not z.
9 years ago
I read this tutorial about overriding equal and hashcode method.

http://javapapers.com/core-java/hashcode-and-equals-methods-override/

I understand how to override equal method, by overriding it, We can custom our compare

I also understand How to override hashcode, To make custom hash.

But still I can not understand why we do it? why if equal method override, we must override hashcode method too?If we don't what is the problem?

To honor the above contract we should always override hashCode() method whenever we override equals() method. If not, what will happen? If we use hashtables in our application, it will not behave as expected. As the hashCode is used in determining the equality of values stored, it will not return the right corresponding value for a key.



Is it the right reason in order to override:

Because when we customize equal method so it focus on special variables,We must change the hash code too in order to match with it, so hashcode also focus on those special variable.

Right?
9 years ago



Matched expression found by findInLine: 34

ef56



\r\n is new line? right?

The java.util.Scanner.nextLine() method advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end.



then 56 is in new line and it must return ef.What is the problem?
9 years ago

I think the source of confusion and main reason why this question has been asked so many times since 2005 is that in java, nothing exists after the last char in a string (i.e. charAt(6) doesn't exist); however, in regex (which java implements via a standard library), a zero-length character does exist after the last char in a string. Maybe the SCJP FAQ article doesn't make this fact very clear.



Yes, That article doesn't make this fact very clear.

Now it's clear for me, Thanks all friends. Thank you
9 years ago

public class Regextest {

public static void main(String a[]){
String stream = "ab34ef";
Pattern pattern = Pattern.compile("\\d*");

//HERE * IS GREEDY QUANTIFIER THAT LOOKS FOR ZERO TO MANY COMBINATION THAT
//START WITH NUMBER
Matcher matcher = pattern.matcher(stream);

while(matcher.find()){
System.out.print(matcher.start());
}
}



But it prints:

012456

9 years ago

Ulf Dittmer wrote:

abalfazl hossein wrote:index starts from zero, and we have six characters.not 7


To put it another way, you have not read the article I pointed you to twice, why not?. You've been visiting this site for years, and have posted hundreds of times, but still you expect other people to do your work for you, when you could learn much faster by simply reading up on stuff - which people even take the time to point you to. I simply don't get that attitude.



I read that article, I add some lines.



run:


indexOf(a) = 0

indexOf(b) = 1
indexOf(f) = 5
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6
at java.lang.String.charAt(String.java:686)
at regextest.Regextest.main(Regextest.java:26)
Java Result: 1




String index out of range: 6

Still has doubt....if it is out of range, Why does it print....
9 years ago
ab34ef

0>a
1>b
2>3
3>4
4>e
5>f

sixth?

index starts from zero, and we have six characters.not 7
9 years ago
What does sixth index point to?

Why doesn't it print 3? 12345
9 years ago


012456



My question is:
The methods start()will give the indexes into the text where the found match starts .
d*, this means if a number found.index must be returned.

Why doesn't it print 3? 12345

Another question:
Why it prints 6?

0>a
.
.
.
5>f

What is this 6 in out put?

Thanks in advance
9 years ago

Campbell Ritchie wrote:And what on earth is that code supposed to do? Did you really find it in the SCJP book? The sort of things you read in some books
It might help you pass the exam, but I think code like that in real life would lose you your job



SCJP Sun certified programmer for java 6

page 505
9 years ago