• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Learning Python in order to learn Java better?  RSS feed

 
Ranch Hand
Posts: 174
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a Jr. Java Developer who has worked on respectable projects, but when it comes to coding interviews, algorithms and puzzles involving the manipulation of arrays, I leave the experience feeling quite humbled and inadequate. I recognize that my problem-solving skills are not up to par.

So, I sat down and created myself a personal project full of puzzlers, and hackerrank.com style solutions that I will try to solve in Java.

It began to occur to me how verbose Java is and how this was detracting from my ability to actually focus on the problem at hand.

So I had this bright idea... that in order to help train my problem-solving skills, I will write/practice my problems in Python, and this will help me become a better problem-solver... a skill which will theoretically transfer over as a Java developer - as they say... the really good developers are language-agnostic.

Is this a good idea? What are some pitfalls with this approach?

 
Ranch Hand
Posts: 150
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really hate how verbose java is compared to C. StringBuilder sb = new StringBuilder(); would be a lot nicer if it was more like this... strBuilder = sb; ... I would get rid of the "new" keyword from the language entirely. It should be implied that we want a new object and that we want the same type we already specified, why write it twice. We should only have to do that if we want a different object type... will practicing python make you better at java? maybe, maybe not, but java will remain verbose and all we can do is get used to it!
 
author & internet detective
Marshal
Posts: 38508
653
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark,
Leanring different languages will make you a better programmer in general. It teaches different ways of thinking about programming and also makes it easier to pick up a third language.

The only "gotcha" is that it is easy to type syntax from the wrong language. For example, sometimes I'll type "def" or "var" when declaring a type in Java. (when I've done a lot of Groovy or JavaScript.) Luckily, the compiler tells me I'm wrong and it is easy to fix.
 
Bartender
Posts: 2180
46
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

S Fox wrote:It should be implied that we want a new object and that we want the same type we already specified, why write it twice.


JEP 286 to the rescue!
 
Sheriff
Posts: 21424
94
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if I read it correctly, that will already be part of Java 10, which is scheduled for March next year.
 
Mark Richardson
Ranch Hand
Posts: 174
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm.... along the same vein,

I found this: https://projectlombok.org/features/

Apparently, it allows using annotations to imply a number of "ceremony" code such as setters and getters, no-arg constructors, etc.

but of course, this may be problematic in some cases, as discussed here:

https://stackoverflow.com/questions/3852091/is-it-safe-to-use-project-lombok
 
Saloon Keeper
Posts: 1807
74
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way to progress could be to lean Kotlin. Here are a few of the features of Kotlin:
  • Runs (nearly?) anywhere that Java does
  • Has much less "ceremony" or boiler plate code
  • You can mix and match Kotlin and Java in the same project
  • Java can call Kotlin and Kotlin can call Java and neither cares what language the code was programmed in.
  • You can find out more about Kotlin here https://kotlinlang.org/
    With you Java base this shouldn't be too big of a leap as you can leverage you Java skills, as well has learn Functional Programming and you can then focus on the problems and problem solving skills.
     
    Marshal
    Posts: 5931
    411
    BSD
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I understand all the fascination about the Kotlin and how it is better than Java, but I need to say Java is a wonderful language with more than wonderful documentation. The other day I was trying to find the way how to populate an array of arrays with Kotlin, and to be honest didn't manage in an easy and painless way, so got enough and picked Java for some task.

    Java has some ceremony, but that explicitness also brings some clarity about the user's intention. What I always used to think is - if you know Java, you can easily pick up various other languages, and if you know some other languages, not necessarily you'll pick up Java easily. I think that's true about Python <--> Java. So I think Java gives a lot to programmers and might even shapes the way one thinks about the programming.

    Scripting languages in particular are difficult to compare with statically typed languages. That ceremony pays off a lot fairly often. You need to get all details in agreement before you can even compile your code, while scripting languages with less ceremony might run in singular case, and horribly fail in another, but you find that out only when that case occurs, so - need a right tool at the right time probably.

    I don't like type inference myself in particular, much more I like to see types specified explicitly, to me that makes code to be easier understandable and more consistent as you see types everywhere rather than somewhere it as inferred, somewhere as explicitly specified.

    Mark Richardson wrote:So, I sat down and created myself a personal project full of puzzlers, and hackerrank.com style solutions that I will try to solve in Java.
    It began to occur to me how verbose Java is and how this was detracting from my ability to actually focus on the problem at hand.


    Puzzles can be challenging and sometimes even annoying. Instead of just concentrating on a problem, you also need to be enough familiar with a language how to achieve solution in an optimum way. However, I think a lot of Java programmers still programming in a habitual way, which is indeed can be verbose, but now that we have Java 8, well, 9 already, you can get a lot from functional programming features Java offer. If you had a chance to become familiar with those, that verbose isn't that verbose anymore, but you need to learn what newer Java versions have to offer. Some puzzles people just solve in a less than optimum way, so they kick themselves into a verbose solution, while there are mathematical approaches, which are much more shorter, but that requires some other specific skills, perhaps in maths, so not everything goes down to Java being a verbose language I think. Anyway, well done trying to improve your problem solving skills.

    Mark Richardson wrote:I will write/practice my problems in Python, and this will help me become a better problem-solver... a skill which will theoretically transfer over as a Java developer


    What definitely you can practice and have transferable is a skill of writting tests. That helps solving puzzles, and that is a must elsewhere. Write tests for each puzzle's decomposed task you trying to tackle, get confidence it does what it does - then move forward. You'll be surprised how it reduces a chaos solving any problems. When you have written and passing tests, you can always come back to your solution and refactor it, make it less verbose, because very first solution which comes to your mind usually isn't the best one.
     
    S Fox
    Ranch Hand
    Posts: 150
    3
    C++ Eclipse IDE Java
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I found some cool programs in python and I'm converting a few to java, just winging it and learning syntax as I go. It's been a very painful process! There's no explicit type declarations for vars, so as I'm looking through the code I don't even know what anything is supposed to be, what the return values are for functions, nothing! And the guy who wrote it is using the longest var names possible. Because python is so non-descript you have to do it, to give a hint about what things are. With java we have so much structure, even brackets which python has ditched, but somehow we still ended up with the super-long var and method names in java. How can java complain that it can't infer types but python can. Plus the picky type-related stuff like int and Integer! Cant cast int to Integer, even though we have autoboxing/unboxing... hah! Python sorts anything you throw at it, but in java we can't sort a hashmap without writing our own comparator? This is probably why we have so many languages, they all seem to have some shortcomings.
     
    Paweł Baczyński
    Bartender
    Posts: 2180
    46
    Firefox Browser IntelliJ IDE Java Linux Spring
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    We can't sort a HashMap because HashMaps aren't meant to be sorted.
    Complaining about it is kind of like complaining that cars can't fly.
     
    S Fox
    Ranch Hand
    Posts: 150
    3
    C++ Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes I know they aren't meant to be sorted, still I see many people on stackoverflow are using hacks and doing it, rather than using TreeMap or etc.
    For my program I was dumping out the k/v pairs as List<Entry<Integer, List<Integer>> but then to sort a list like that I don't think we can use the natural sorting method for lists or collections, I had to make a comparator.
     
    Ranch Hand
    Posts: 239
    12
    Eclipse IDE IntelliJ IDE Java Scala Ubuntu
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hey Mark, If your goal is to learn better problem solving skills, what if you dispensed with the languages altogether? I think you may only need a pencil and paper (or a whiteboard and marker) and go solve those problems without the overhead of the language at all. Use pseudocode as necessary. When you're confident in your solution, then come back to the keyboard and code it up and see. This approach has always worked better for me.
     
    Mark Richardson
    Ranch Hand
    Posts: 174
    2
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Scott Shipp wrote:what if you dispensed with the languages altogether? I think you may only need a pencil and paper.



    Scott, that's a wonderful idea and gets to the core of what I'm trying to accomplish. I actually started watching this MIT course:

    MIT 6.006 Introduction to Algorithms, Fall 2011 (https://www.youtube.com/watch?v=HtSuA80QTyo)

    Even though it's an introductory course, it apparently has some pre-requisites (Engineering courses), which as a Java developer I don't necessarily want to invest time in. It starts mentioning concepts such as asymptotic analysis, which are somewhat new to me.

    As a non-CS major, I keep hitting this realization that where I am trying to get at requires for me to put in 4 years studying CS at a university. As an optimist, I know there's an easier way to make up for this, but I just need to find the right resources - after all, I taught myself Java

    So, I guess the next question would be: Where to learn these skills without it being too "academic" of an endeavor?
     
    S Fox
    Ranch Hand
    Posts: 150
    3
    C++ Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    After using python a week I have found that solving problems in java and python can be very different in the implementation. Sometimes its nearly the same. Other times a single line of python can accomplish what 5 long lines of java does. So I think it's best to stick with java if you want to learn java, there's too many differences of implementation.
     
    Bartender
    Posts: 19668
    92
    Android Eclipse IDE Linux
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I used to "collect" programming languages until the rise of the Internet spawned such a wave of languages that I simply couldn't keep up with them all. Just a basic Red Hat Linux system install has code in C, Python, Perl, Bash, and probably Go and those are just the ones that come immediately to mind. Probably some C++ in there, too. My Puppet resources are in Ruby. The Emacs macros are in LISP.

    Each language has its strong and weak points, and there's a lot of similarities as well. Many languages have syntax that is C-like (for example, Java) or Pascal-like. Then there are the BASIC-like languages such as Python. Followed by languages that are more or less self-defined (such as Forth and Smalltalk), miscellaneous (LISP, Scheme), and the truly bizarre such as APL.

    You can carry over some basic concepts, such as conditional logic concepts (IF/THEN/ELSE, LOOP), which may vary in details (for example, SWITCH/CASE versus ELIF). Most languages have expressions and subexpressions (BEWARE: Operator precedence may vary!). Likewise some sort of subroutine and/or function call. And often there are local quirks like lambdas, generic iterators [i]etc.[/] APL even included matrix operators.

    But that's only part of it. Language environments vary from the original C and FORTRAN or COBOL minimalist services to the rich and complex class libraries of Java and Python. Which reminds me there's also a difference between straight procedural, structured programming and object-oriented systems (and some hybrids such as C++).

    So learning one language as an aid to learning another isn't likely to work too well. In fact, the original Java certification tests were heavy on "gotcha" questions designed specifically to trip up people who were familiar with C and C++. Python packages are quite unlike Java packages, not only in coding but in how they can/must be laid out in the filesystem. And one thing Python continually bites me on is the need to explicitly reference "self" in class member functions.

    Best if you need to learn one language now to learn that language. Learn others later.
     
    S Fox
    Ranch Hand
    Posts: 150
    3
    C++ Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    YEP!!! I just found out that the mod operator of python does not work correctly. After spending all day debugging the same section of code, I finally figured out that python has just arbitrarily changed the rules of their math operators. What a pain in the ass! This isn't operator precedence change, they went and made mod operations work differently for negative numbers. So python doesn't give the same result as every calculator on the planet let alone other languages.
     
    Ranch Hand
    Posts: 96
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    S Fox wrote:YEP!!! I just found out that the mod operator of python does not work correctly. After spending all day debugging the same section of code, I finally figured out that python has just arbitrarily changed the rules of their math operators. What a pain in the ass! This isn't operator precedence change, they went and made mod operations work differently for negative numbers. So python doesn't give the same result as every calculator on the planet let alone other languages.



    Dear Sir,

    It is the other way, Python mod works correctly, the % in Java returns just the remainder of a division. They are indeed different operations.

    Thanks.
     
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The biggest difference between the two languages is that Java is a statically typed and Python is a dynamically typed.
    Python is strongly but dynamically typed. This means names in code are bound to strongly typed objects at run-time.
    Both languages are compiled down to byte codes that run on virtual machines, although Python generally does this automatically at run-time and Java has a separate program  that does it.
     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!