praveen kumaar

Ranch Hand
+ Follow
since Apr 10, 2016
praveen likes ...
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
Merit badge: grant badges
For More
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by praveen kumaar

Pedro Esgueira wrote:
With my "if" I don'I mean to describe an hypothetical sitaution - I'm just describing what seems to me the order of initialization acording to the author's words "If you answered 5, you got it right. Fields and blocks are run first in order, setting number to 3 and then 4. Then.."
Which then leads me to ask: "what is then the role of the main method" (because, from my interpretation of the authors' words, they seem to not be starting with the main method)...

Well, it's getting confusing for me. best thing would be to post a complete code as you like to be in class Egg, and separately, show the order of execution by line numbers as per your thinking. that way, perhaps you will get a better help..
4 years ago

sohail wrote:assignment requires runtime check, so an integer object reference is assigned to int which is primitive type, autoboxing and unboxing can be done at while declaring the variable not at the assignment so error at runtime because if it not initialized it contain null
So null is assigned to int variable nullpointerexception ...

Hi sohail, Autoboxing and Unboxing does applies to both case, initialization and assignment. The actual reason for the NullPointerException there is that: compiler produces(optimize) a code that actually looks something like:
so if you will try something like null.someInstanceMethod() it will cause such exception.
And for Autoboxing compiler does transform the code to something like:
You can verify it, if you have some idea about javap command...
4 years ago
I am sorry, but the original question that the OP is talking about is about this code:

My previous post covers the context of the question asked by OP.

Pedro Esgueira wrote:What is then the role of the main method if we don't start with it, and instead start with

Main is the entry point where actually execution of your code really begins. if you will not have a main method then how can you say where should the code starts executing. java is a OOPS based and saying something like how would it be if we start from some line like you said does not really makes a sense because those lines are part of an object and for executing them we must be able to create objects, but the question comes where can we create those objects, so we need some starting point like a main method(one of the rules made by java). Think about it.

And I have covered the another similar example(by mistake) from the same page in my previous post.
4 years ago
Here is the complete code from the book Oracle Certified Assosciate Java SE 8 Programmer 1 Study Guide By Jeanne Boyarsky and Scott Selikof

Then authors modified some initial lines to:

Well What they really means for the order is: just after when the new operator is invoked on the class the construction process for an object starts, where the initialization and instance initializer blocks are executed first in the order they are written. so in the first case where the code compiles, the variable name  is initialized before the instance initializer block and thus if we try to access the variable further it will be the value with which it was initialized. however in the next case the instance initializer block will execute first where it is trying to access the variable which is not even declared(or initialized) yet so the compiler will remind us for this(compiler really tries its best to caught some mistakes as early as possible).

For MAIN: yes the code starts right there but when it sees the new is invoked on the constructor, it gets the type of the object it needs to construct and thus before calling the constructor there are some setups applied on the object being constructed like the case we see.

[EDIT]: I will give you some more examples of those setups,  but lets see some more code and try to guess it's output:

Here is the output:

When you give the command(after successful compilation) for running the code: Java Chick.

Here are the things that will happen:

  • Before the invocation of main the class will be loaded(in which main is defined).
  • Loading a class causes the initialization of class variables and static initializer blocks to run in the order they are encountered from top to bottom in class.
  • Please note the static initilaizers(if any) in the super class will be executed before the child class.
  • During execution of one of the static initializer blocks i have created an instance variable inside it, which will cause the execution of instance initializer blocks and instance variable initialization. thereafter the constructor call will execute.
  • Please note: i have called the instance method hello() inside the instance initializer block which is executed before the construcor() which means the dynamic binding of methods with the object is happening before the constructor completes.
  • Notice the value of static variable i, which is printed twice in static initializer block, and think about, why the value 0 and 1 is printed.

  • If you are interested to dig deep in this topic here is the reference to the appropriate section in Java Language Specification: Execution.

    4 years ago

    User Kunal wrote:Oh I have another doubt. Can you please answer my very first post?

    Campbell has all ready given very clear explanation for your 1st post which is indeed, the very first post of this thread. In case, if it's something else, go and start a separate thread. You should take a look at How to ask questions on Java Ranch, as the way you ask to answer the question is not a good example for asking questions here.
    4 years ago
    Well like other methods in java, next() will be called when the control reaches the line where it is invoked. input may or may not be present prior to invoking the next(). And as per docs, the call to this method may block for scanning the input. Generally for files, the method doesn't seems like blocking and in the case of standard input(from terminal) the scanner blocks the call waiting for the input.
    4 years ago
    Don't use static variables and static methods just for getting rid of creating objects. That way, you are actually keeping yourself away from the intent of OOPS paradigm.

    So basically you need a value from a user from some method and wants to pass it to other for some processing. In your code it's not working because you have not called the other method(isEven, which needs to do the processing of user input).In java for executing some method you have to call it in some context like the main method(or some other one). and please note their is not any relation b/w the parameter declared in method, isEven(named as userNumber), and the one declared as a class static variable(also named as userNumber). You can indeed name them something else different from each other.
    you can call isEven as shown, in your main method:
    4 years ago
    Hi Neha,
    In both the cases Please look carefully at your code in finally block and the code following it. In your first case you are returning a value from the finally block. and in your second case you are returning a value outside and after a finally block.Actually during an execution of try-catch-finally block when the try block reaches a code throwing an exception, the following things happen in your cases:

    1) Look up for a catch block for the exception thrown, you did not have one.
    2) Control is passed to the finally block for execution, where you actually return a value and thus got the result during a call to the method.
    3) if you have not returned this value in finally bock then the control would have again gone to the try block for throwing the exception to the caller which actually happens in your 2nd case.

    In your 2nd case the statement after a finally block is not reachable because your try block has not completed normally. it will only be reachable if your try-catch-finally block will complete normally.

    My notes are for your specific use case. If you need detailed description, here is one from Java Language Specification, Section 14.20.2.


    Yan Digilov wrote:Ah, sorry yall, I am talking about the section ABOVE the sharpen your pencil piece.  I mean the one titled "Assignment".  It goes "Double d = x;" and there is an Integer and an int pointing to it...

    I think taking a step to compile that simple line is not a big deal. Either it's an error or not, compiler is the best one which will tell you about it, it's one of the best core task assigned to it. but it's well likely for a compiler to not caught the errors which will really happen during run time as those are generated dynamically. in that case it's again not a following big step to run that program to be sure about the behaviour of code is, as i expected.
    Now experiment the same code with a java as mentioned in the book and think about what really goes behind the scene?

    Ask yourself:

    1.) if not compiled, why really it didn't compile, what's wrong have you done there...
    2.) if compiled but not ran, what's really something that compiler has not really caught. and the most important the error, why does it appear?
    3.)if compiled and run successfully,  why can i assign int to a Double reference. what really is going behind the scene? Is there any optimization added by compiler, if so then what are they.

    Tell us your views first, then we will see if their are any problems.
    5 years ago

    Rafael wrote:Do you know of any java project, preferably some application with social objective, where you can collaborate as a programmer on a voluntary basis?

    Welcome to the Ranch, Rafael.
    Hmmm social objective, can you be more specific as it's a relative term.what is your perspective ? Do you mean open source, their are many on GitHub, Go and explore them as per your requirement.

    Note: The link i have attached contains a variety of projects which are both as large as JUnit and small as  a HelloWorld Example.

    Hope it helps!

    5 years ago
    Congratulations! Mike.
    5 years ago

    Shubham Rai wrote:Intersection means common characters in all the given strings.

    Shubham, it would be a lot better if you can post the original language in the problem. please don't forget to mention the source(for copyright issues). if it's from some book tell us the book name, page number etc. if you have a link to the original problem you have an option to cite the link also. We really want to help but we need to understand the problem first. As you can see in the previous discussion of this thread, i have guessed something and campbell has something else which is creating ambiguity.
    And I think you should have a look at HowToAskQuestions.
    5 years ago
    List1.retainAll(List2.retainAll(List3)) -> I am sorry but it will fortunately fail as retainAll returns a boolean. it should have been
    In plain english it means first retain only the elements in list1 which are present in list2 and then further again  retain olnly those elements in list1 that are present in list3. where list1, list2, list3 should be a List of Characters.
    5 years ago

    Campbell Ritchie wrote:What's an intersection character?

    I mean characters common among all the String and if their is some character common among the input strings increment the count by 1 additionally if some counted character repeats in any string then it will be counted again if the same character repeats also in all other strings. That's a guess though. let the OP decide if it's what he wants.[Edited]
    5 years ago

    Campbell Ritchie wrote:Please explain exactly how you are counting letters. I can see c in all three Strings, so I would expect the answer 3.

    I think he just needs the number of intersection characters of the n strings..
    5 years ago