Web Hosting How-Tos

  Home arrow Web Hosting How-Tos arrow Page 2 - Learning a New Programming Language Pa...
Web Hosting Articles  
Web Hosting FAQs  
Web Hosting How-Tos  
Web Hosting News  
Web Hosting Reviews  
Web Hosting Security  
Weekly Newsletter 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Contact Us 
Site Map 
Privacy Policy 
  >>> SIGN UP!  
  Lost Password? 

Learning a New Programming Language Part 3: Syntax Differences
By: Chris Root
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 8

    Table of Contents:
  • Learning a New Programming Language Part 3: Syntax Differences
  • Defining Variables
  • Variable Syntax
  • Defining Functions
  • A Few Words About C and C related languages
  • Let the Parrot Speak
  • A Summary

  • Rate this Article: Poor Best 
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article




    Learning a New Programming Language Part 3: Syntax Differences - Defining Variables

    (Page 2 of 7 )

    Having some sort of namable container for data in a programming language is a no brainer. What varies from one language to another is the way they are created and the syntax that is used for them. This brings us to a major concept in programming, called typing.

    When you are packing things in boxes to move somewhere, you usually try to use the right box for the items you wish to pack. If you want to pack a small item you generally don't use a large container; if you have a large item, you can't very well use a container that is too small.

    This idea is enforced in many languages through typing (as in the type of data stored). Some languages have what is called "static" typing. In static typing you must specify when declaring a variable what type of data you intend to store there.

    If you are used to a language that has dynamic typing such as Javascript, you probably never thought much about types. In languages that use dynamic typing, the language interpreter takes care of handling data types for you. Sometimes you do have the option of specifying type if you really need to.

    Java is an example of a statically typed language. In Java, variables, including arguments to methods and return values from methods, must be declared with a type. Since methods use the keyword "return" to return values, the type for the return value is specified before the method name.

    int myMathMethod(int myNumber)


    return myNumber + 8;


    int someNumber;

    int result;

    someNumber = 6;

    // call the method

    result = myMathMethod(someNumber);

    //result in this case would be 14

    It is good practice when coding a class in pretty much any language to declare and then assign values. Declarations are usually done at the top of the file, while assignments are done anywhere you need them.

    The type is specified first, followed by the name of the variable. In this case all the types are "int" or integers. There are a few types such as integers, double, long, float, boolean and char that are pretty common among most programming languages. Some languages have more types than others.

    Wether or not this kind of rigid adherence to typing data actually affects the memory footprint of an application depends on the compiler or interpreter. Static typing has the benefit of exposing potentially fatal (fatal to your application, not to you of course) bugs in your code when data gets used in an erroneous way.

    What do I mean? Well, say for instance that, in the midst of some complicated code, an integer variable is multiplied by a string. Whatever output, if any, you might get from such an equation would probably be of no use to you.

    You wouldn't knowingly code things this way, but if you were to accidentally reassign a string to a variable that had been an integer, and then later use it in that equation, static typing rules would catch the error.

    Weak vs. Strong Typing

    Okay, so what if you want to take a number generated from your code and make it part of a string that can be displayed to the user?

    string msg = "You have " + mynumber + " messages";

    print msg;

    This a perfectly logical mixing of data types, but would it work? In a language that uses strong typing it would not. With strong typing you must explicitly convert data from one type to another before you can use it in this way. In this example you would have to convert the "mynumber" integer variable into a string. Weakly typed languages are more forgiving and attempt to make the conversion for you.

    Thinking about types does take a little getting used to if you haven't had to before (we are creatures of habit after all) but it can help keep you up on hard to track bugs in your code. On the other hand, I have known a few people with particularly good memories that seem to be able to keep what variable is what in their head even with large projects. Go figure.

    More Web Hosting How-Tos Articles
    More By Chris Root


    - Phishing Scams: An Overview and How to Detec...
    - Tips for Safe Downloading Online
    - How To Avoid Spam
    - How to Get Into Ethical Hacking
    - How to Prevent Drive-by Downloads
    - Facebook Timeline Tips and Tricks
    - How to Keep Up with Facebook`s Changes
    - Wi-Fi Network Security Tips
    - Tips for Safe Online Holiday Shopping
    - Facebook Privacy: Keeping Up with the Const...
    - Tips for Facebook Privacy
    - How to Cover Your Tracks on the Web
    - SSH Keys for FileZilla and Putty in Cpanel
    - How to Create a Filezilla FTP User
    - How to Install FileZilla Server

    Developer Shed Affiliates


    © 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap